[Prev: Built-in Objects] [Home] [Next: Qt]

Math

The Math object always exists in a program. Use the Math object to access mathematical constants and functions, e.g.

    with ( Math ) {
        var x = PI * 2;
        var angle = 1.3;
        var y = x * sin( angle );
    }

The Math object supports all the common mathematical functions, for example, abs(), acos() and cos(), asin() and sin(), atan(), atan2() and tan(), ceil(), floor() and round(), exp() and log(), max() and min(), pow() and sqrt(), random(), and round().

See also, + operator, ++ operator, - operator, -- operator, * operator, / operator, % operator, -= operator, += operator, *= operator, /= operator and %= operator.

Math Properties

All the Math properties are read-only constants.

Math Functions
abs()

abs( number )

    var x = -99;
    var y = 99;
    with ( Math ) {
        x = abs( x );
        y = abs( y );
    }
    if ( x == y ) System.println( "equal" );

Returns the absolute value of the given number. The equivalent of

    x = x < 0 ? -x : x;
acos()

acos( number )

Returns the arccosine of the given number in radians between 0 and Math.PI. If the number is out of range, returns NaN.

See also cos().

asin()

asin( number )

Returns the arcsine of the given number in radians between -Math.PI/2 and Math.PI/2. If the number is out of range, returns NaN.

See also sin().

atan()

atan( number )

Returns the arctangent of the given number in radians between -Math.PI/2 and Math.PI/2. If the number is out of range, returns NaN.

See also tan() and atan2().

atan2()

atan2( yCoord, xCoord )

Returns the counterclockwise angle in radians between the positive x-axis and the point at (xCoord, yCoord). The value returned is always between -Math.PI and Math.PI.

Example:

    function polar( x, y )
    {
        return Math.atan2( y, x );
    }

See also tan() and atan().

ceil()

ceil( number )

If the number is an integer, it returns the number. If the number is a floating point value, it returns the smallest integer greater than the number.

Example:

    var x = 913.41;
    x = Math.ceil( x ); // x == 914
    var y = -33.97;
    y = Math.ceil( y ); // y == 33

See also floor() and round().

cos()

cos( number )

Returns the cosine of the given number. The value will be in the range -1..1.

See also acos().

exp()

exp( number )

Returns Math.E raised to the power of the given number.

See also log().

floor()

floor( number )

If the number is an integer, it returns the number. If the number is a floating point value, it returns the greatest integer less than the number.

See also ceil() and round().

log()

log( number )

If the number is > 0, it returns the natural logarithm of the given number. If the number is 0, it returns Infinity. If the number is < 0, it returns NaN.

See also exp().

max()

max( number1, number2 )

Returns the largest of number1 and number2.

See also min().

min()

min( number1, number2 )

Returns the smallest of number1 and number2.

See also max().

pow()

pow( number, power )

Returns the value of the number raised to the power.

See also sqrt().

random()

random()

Returns a pseudo-random floating point number between 0 and 1. Pseudo random numbers are not truly random, but are often adequate for many applications, for example, games and simulations.

round()

round( number )

Returns the number rounded to the nearest integer. If the fractional part of the number is >= 0.5, the number is rounded up; otherwise it is rounded down.

See also ceil() and floor().

sin()

sin( number )

Returns the sine of the given number. The value will be in the range -1..1.

See also asin().

sqrt()

sqrt( number )

If the number is >= 0, it returns the square root. If the number is < 0, it returns NaN.

See also pow().

tan()

tan( number )

Returns the tangent of the given number.

See also atan() and atan2().

[Prev: Built-in Objects] [Home] [Next: Qt]