Math

class MathAvailable in flash8, flash, neko, js, php, cpp, cs, javaThis class defines mathematical functions and constants.
All angle-based functions use radians. If you need to convert radians to degrees or back, look for corresponding functions in the comments section.
static var NEGATIVE_INFINITY(default,null) : FloatA constant to specify negative infinity (−∞).
http://en.wikipedia.org/wiki/Extended_real_number_line
static var NaN(default,null) : FloatFloat value that is Not a Number.
http://en.wikipedia.org/wiki/NaN
static var PI(default,null) : FloatMathematical constant Pi (approximately 3.14159).
http://en.wikipedia.org/wiki/Pi
static var POSITIVE_INFINITY(default,null) : FloatA constant to specify positive infinity (+∞).
http://en.wikipedia.org/wiki/Extended_real_number_line
static function abs( v : Float ) : FloatCalculates the absolute value of the specified number.
http://en.wikipedia.org/wiki/Absolute_value
static function acos( v : Float ) : FloatCalculates arccosine of the specified angle in radians.
Returns a Float in [-1.0, 1.0] range.
http://en.wikipedia.org/wiki/Inverse_trigonometric_functions
static function asin( v : Float ) : FloatCalculates arcsine of the specified angle in radians.
Returns a Float in [-1.0, 1.0] range.
http://en.wikipedia.org/wiki/Inverse_trigonometric_functions
static function atan( v : Float ) : FloatCalculates arctangent of the specified angle in radians.
Returns a Float in [-Math.PI / 2, Math.PI / 2] range.
http://en.wikipedia.org/wiki/Inverse_trigonometric_functions
static function atan2( y : Float, x : Float ) : Floathttp://en.wikipedia.org/wiki/Atan2 static function ceil( v : Float ) : IntCalculates the ceiling of the specified number (rounds up to the nearest integer).
Example:
trace(Math.ceil(0.3)); // 1
trace(Math.ceil(1.0)); // 1
trace(Math.ceil(-2.5)); // -2
http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
static function cos( v : Float ) : FloatCalculates the cosine of the specified angle in radians.
Returns a Float in [-1.0, 1.0] range.
static function exp( v : Float ) : Float static function fceil( v : Float ) : Float static function ffloor( v : Float ) : Float static function floor( v : Float ) : IntCalculates the floor of the specified number (rounds down to the nearest integer).
Example:
trace(Math.floor(0.7)); // 0
trace(Math.floor(1.0)); // 1
trace(Math.floor(-2.5)); // -3
http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
static function fround( v : Float ) : Float static function isFinite( f : Float ) : BoolReturns true if the specified Float is neither POSITIVE_INFINITY nor NEGATIVE_INFINITY.
Returns true for NaN.
trace(Math.isFinite(Math.NaN)); // true
trace(Math.isFinite(Math.PI)); // true
trace(Math.isFinite(Math.POSITIVE_INFINITY)); // false
static function isNaN( f : Float ) : BoolReturns true if the specified Float is NaN. static function log( v : Float ) : FloatCalculates the natural logarithm of the specified number (ln(x)).
http://en.wikipedia.org/wiki/Natural_logarithm
static function max( a : Float, b : Float ) : FloatReturns the biggest of the specified numbers. static function min( a : Float, b : Float ) : FloatReturns the smallest of the specified numbers. static function pow( v : Float, exp : Float ) : FloatCalculates power of the specified number.
trace(Math.pow(2, 4)); // 16 = 2 * 2 * 2 * 2
trace(Math.pow(1.5, 2)); // 2.25 = 1.5 * 1.5
trace(Math.pow(1, 3)); // 1 = 1 * 1 * 1
http://en.wikipedia.org/wiki/Exponentiation
static function random() : FloatReturns a pseudo-random Float in the [0,1) range, ie. 0 <= Math.random() < 1. For random Int see Std.random(). static function round( v : Float ) : IntRounds specified value to the nearest integer. Rounds half-way values up.
Example:
trace(Math.round(0.49)); // 0
trace(Math.round(0.5)); // 1
trace(Math.round(0.9)); // 1
trace(Math.round(-1.5)); // -1
http://en.wikipedia.org/wiki/Rounding
static function sin( v : Float ) : FloatCalculates the sine of the specified angle in radians.
Returns a Float in [-1.0, 1.0] range.
http://en.wikipedia.org/wiki/Sine
static function sqrt( v : Float ) : FloatCalculates the square root of the specified number.
Example:
trace(Math.sqrt(25)); // 5
trace(Math.sqrt(4)); // 2
http://en.wikipedia.org/wiki/Square_root
static function tan( v : Float ) : FloatCalculates the tangent of the specified angle in radians.
http://en.wikipedia.org/wiki/Tangent
version #19312, modified 2013-05-08 11:13:36 by api
4 comments
  • Nov 24, 2008 at 13:19

    It would be great to have a function "clamp(value : Float, min : Float, max : Float) : Float" that would clamp the value against the given bounds.

    Something like that:
    ''public static function clamp(value : Float, min : Float, max : Float) : Float
    {
    if (value < min)
    return min;
    else if (value > max)
    return max;
    else
    return value;
    }''

    And it could also be great to have an Int version of min(), max() and clamp() (that could be called minInt(), maxInt() and clampInt())

  • Ramūnas Gutkovas
    Dec 10, 2008 at 16:07

    Ahrr..

    maybe more slow (but I think compiler should easily inline these), bet certainly nicer :)

    public static function clamp(value : Float, min : Float, max : Float) : Float {
    return min(max(min, value), max);
    }

  • Jun 22, 2012 at 14:19

    I second the Clamp addition.

    + rad2deg / deg2rad (radians / degrees conversion) functions would be very useful (probably optimised to inlines or something).

  • Jun 22, 2012 at 14:27
    /**
    * Converts specified angle in radians to degrees.
    * @return angle in degrees (not normalized to 0...360)
    */
    public inline static function radToDeg(rad:Float):Float
    {
        return 180 / Math.PI * rad;
    }
    /**
    * Converts specified angle in degrees to radians.
    * @return angle in radians (not normalized to 0...Math.PI*2)
    */
    public inline static function degToRad(deg:Float):Float
    {
        return Math.PI / 180 * deg;
    }
    /**
    * "Clamps" a value to boundaries [min, max].
    * Example:
    * clamp(2, 1, 5) == 2;
    * clamp(0, 1, 5) == 1;
    * clamp(6, 1, 5) == 5;
    */
    public inline static function clamp(value:Float, min:Float, max:Float):Float
    {
        if (value < min)
            return min;
        else if (value > max)
            return max;
        else
            return value;
    }