Math

You are viewing an old version of this entry, click here to see latest version.
class MathAvailable in flash8, flash, neko, js, php, cppThis class defines mathematical functions and constants. static var NEGATIVE_INFINITY(default,null) : Float static var NaN(default,null) : FloatFloat value that is Not a Number.
http://en.wikipedia.org/wiki/Not_a_number
static var PI(default,null) : FloatMathematical constant Pi (approximately 3.14159).
http://en.wikipedia.org/wiki/Pi
static var POSITIVE_INFINITY(default,null) : Float 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 ) : Float static function asin( v : Float ) : Float static function atan( v : Float ) : Float static function atan2( y : Float, x : Float ) : Float 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
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 floor( v : Float ) : Int static function isFinite( f : Float ) : Bool static function isNaN( f : Float ) : Bool static function log( v : Float ) : Float static function max( a : Float, b : Float ) : Float static function min( a : Float, b : Float ) : Float static function pow( v : Float, exp : Float ) : Float 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 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
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
static function tan( v : Float ) : FloatCalculates the tangent of the specified angle in radians.
http://en.wikipedia.org/wiki/Tangent
version #14292, modified 2012-06-22 14:54:50 by zlumer
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;
    }