Math

The Math namespace contains math methods and constants.

static property Float Math.Eread only

The value for e, the base of the natural logarithms, which is approximately 2.718.

static property Float Math.LN2read only

The value for the natural logarithm of 2, which is approximately 0.693.

static property Float Math.LN10read only

The value for the natural logarithm of 10, which is approximately 2.302.

static property Float Math.LOG2Eread only

The value for the base-2 logarithm of e, which is approximately 1.442.

static property Float Math.LOG10Eread only

The value for the base-10 logarithm of e, which is approximately 0.434.

static property Float Math.PIread only

The value for the number Pi, the ratio of the circumference of a circle to its diameter, which is approximately 3.141.

static property Float Math.PHIread only

The value for the mathematical golden ratio, which is approximately 1.618.

static property Float Math.SQRT1_2read only

The value for the square root of 1/2, which is approximately 0.707.

static property Float Math.SQRT2read only

The value for the square root of 2, which is approximately 1.414.

static property Float Math.NaNread only

Not a number.

static property Float Math.Infinityread only

Positive infinity.

static function Math.abs(Int value) → Int

static function Math.abs(Float value) → Float

Returns the absolute (positive) value of a number.

Math.abs(-1)   // 1
Math.abs(-100) // 100
Math.abs(2.2)  // 2.2

static function Math.acos(Float x) → Float

Computes the principle value of the arc cosine of the provided argument. The returned value is in degrees.

static function Math.asin(Float x) → Float

Computes the principle value of the arc sine of the provided argument. The returned value is in degrees.

static function Math.atan(Float x) → Float

Computes the principle value of the arc tangent of the provided argument. The returned value is in degrees.

static function Math.atan2(Float y, Float x) → Float

Computes the principal value of the arc tangent of y/x, using the signs of both arguments to determine the quadrant of the return value. The returned value is in degrees.

static function Math.ceil(Float value) → Float

Returns the value rounded up to the nearest whole number.

Math.ceil(2)    //  2
Math.ceil(3.2)  //  4
Math.ceil(3.8)  //  4
Math.ceil(-2.3) // -2

static function Math.clamp(Float min, Float value, Float max) → Float

Clamps a number between a minimum and maximum value

This method takes 3 arguments. A minimum value, a value to be clamped and a maximum value.

Math.clamp( 4, -200, 98)          //  4
Math.clamp( -2, rectangle.x, 200) // -2 as rectangle.x was -100
Math.clamp(0, 50, 100)            //  50

static function Math.cos(Float degrees) → Float

Computes the cosine of degrees.

static function Math.exp(Float value) → Float

Computes the base-e exponential of value.

static function Math.floor(Float value) → Float

Returns the value rounded down to the nearest whole number.

Math.floor(2)    //  2
Math.floor(3.2)  //  3
Math.floor(3.8)  //  3
Math.floor(-2.3) // -3

static function Math.log(Float value) → Float

static function Math.log2(Float value) → Float

static function Math.log10(Float value) → Float

The log() function computes the natural logarithm of value.

The log2() function computes the logarithm of value to base 2.

The log10() function computes the logarithm of value to base 10.

static function Math.mapRange(Float value, Float fromStart, Float fromEnd, Float toStart, Float toEnd) → Float

static function Math.min(number value1, number value2...) → number

Returns the lowest number from zero or more arguments.

Math.min(10, 20);               //  10
Math.min(10, root.width, 20)    //  10 as root.width = 100
Math.min(-10, -20);             // -20
Math.min(-10, 20);              // -10

static function Math.max(number value1, number value2...) → number

Returns the highest number from zero or more arguments.

Math.max(10, 20);               //  20
Math.max(10, root.width, 20)    //  100 as root.width = 100
Math.max(-10, -20);             // -10
Math.max(-10, 20);              //  20

static function Math.mean(number value1, number value2...) → Float

Returns the arithmetic mean of the provided values.

static function Math.pow(Float base, Float exponent) → Float

Returns the base to the exponent. This is equivalent to the code base^exponent.

Math.pow(1, 10) // 1
Math.pow(2, 10) // 1024
Math.pow(-3, 5) // -243

static function Math.random() → Float

static function Math.random(Float high) → Float

static function Math.random(Float low, Float high) → Float

Generates random numbers.

If no parameters are provided, it will return a float in the range between zero and one.

If only one parameter is passed to the function, it will return a float between zero and the value of the high parameter. For example, random(5) returns values between 0 and 5 (starting at zero, and up to, but not including, 5).

If two parameters are specified, the function will return a float with a value between the two values. For example, random(-5, 10.2) returns values starting at -5 and up to (but not including) 10.2. To convert a floating-point random number to an integer, use the Math.floor() function.

Math.random(20)      // Value between 0.0 and 20
Math.random(-10, 10) // Value between -10 to 10

static function Math.round(Float value) → Float

Returns the value rounded to the nearest whole number.

Math.round(3.4999)  //  3
Math.round(3.5)     //  4
Math.round(-2.1)    // -2

static function Math.sin(Float degrees) → Float

Computes the sine of degrees.

static function Math.sqrt(Float value) → Float

Computes the square root of value.

static function Math.tan(Float degrees) → Float

Computes the tangent of degrees.