[Prev: Built-in Objects] [Home] [Next: System]
The Math object always exists in a Qt Script 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.
All the Math properties are read-only constants.
E -- Euler's constant. The base for natural logarithms.
LN2 -- Natural logarithm of 2.
LN10 -- Natural logarithm of 10.
LOG2E -- Base 2 logarithm of E.
LOG10E -- Base 10 logarithm of E.
PI -- Pi.
SQRT1_2 -- Square root of 1/2.
SQRT2 -- Square root of 2.
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( 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( 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( 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.
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 ); }
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
cos( number )
Returns the cosine of the given number. The value will be in the range -1..1.
See also acos().
exp( number )
Returns Math.E raised to the power of the given number.
See also log().
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.
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( number1, number2 )
Returns the largest of number1 and number2.
See also min().
min( number1, number2 )
Returns the smallest of number1 and number2.
See also max().
pow( number, power )
Returns the value of the number raised to the power.
See also sqrt().
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( 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.
sin( number )
Returns the sine of the given number. The value will be in the range -1..1.
See also asin().
sqrt( number )
If the number is >= 0, it returns the square root. If the number is < 0, it returns NaN.
See also pow().
tan( number )
Returns the tangent of the given number.