The Math Object

The Math object is a single object that has some predefined properties and functions in its prototype.

There is no Constructor object for the Math object and it is not possible to create a Math object. You use the Math object by just referencing its properties or functions.

e.g.

var circumference = diameter * Math.PI;

var smallest = Math.min(arg1, arg2, arg3);

Shown below is the Math object prototype chain.

The Math object is of class Math.

Math Object Properties

All Math object properties are read only (they can not be changed or deleted). It is not possible to add new properties to the Math object.

Property

Description

prototype

A reference to the Math Prototype. This property does not enumerate and can not be changed or deleted.

Math Object Functions

The Math object has no predefined functions.


The Math Prototype

This prototype can be used by any object.

Math Prototype Properties

All Math prototype properties, except the prototype, are read only (they can not be changed or deleted).

Property

Description

E

Euler's constant and the base of natural logarithms, approximately 2.7182818284590452354.

LN10

Natural logarithm of 10, approximately 2.302585092994046.

LN2

Natural logarithm of 2, approximately 0.6931471805599453.

LOG10E

Base 10 logarithm of E, approximately 0.4342944819032518.

LOG2E

Base 2 logarithm of E, approximately 1.4426950408889634.

PI

Ratio of the circumference of a circle to its diameter, 3.1415926535897932.

SQRT1_2

Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.7071067811865476.

SQRT2

Square root of 2, approximately 1.4142135623730951.

prototype

A reference to the Object Prototype. This property does not enumerate and can not be deleted but can be changed.

Math Prototype Functions

Every function listed in this section converts each of its arguments to a number value (in left-to-right order if there is more than one) and then performs a computation on the resulting number value(s).

Where the description defines the result as an approximation it is because the Java Virtual Machine is being used for calculation and the algorithms used are to some extent platform dependant. Script calculations using these functions will return the same results as Java calculations.

Function

Description

abs(x)

Returns the absolute value of x. The result has the same magnitude as x but has positive sign.

acos(x)

Returns an approximation to the arc cosine of x. The result is expressed in radians and ranges from +0 to +pi

asin(x)

Returns an approximation to the arc sine of x. The result is expressed in radians and ranges from -pi/2 to +pi/2.

atan(x)

Returns an approximation to the arc tangent of x. The result is expressed in radians and ranges from -pi/2 to +pi/2.

atan2(y, x)

Returns an approximation to the arc tangent of the quotient y/x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result.

Note that it is intentional and traditional for the two-argument arc tangent function that the argument named y be first and the argument named x be second. The result is expressed in radians and ranges from -pi to +pi.

ceil(x)

Returns the smallest (closest to -infinity) number value that is not less than x and is equal to a mathematical integer. If x is already an integer, the result is x.

Note: The value of Math.ceil(x) is the same as the value of -Math.floor(-x).

cos(x)

Returns an approximation to the cosine of x. The argument is expressed in radians.

exp(x)

Returns an approximation to the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).

floor(x)

Returns the greatest (closest to +infinity) number value that is not greater than x and is equal to a mathematical integer. If x is already an integer, the result is x.

Note: The value of Math.floor(x) is the same as the value of -Math.ceil(-x).

log(x)

Returns an approximation to the natural logarithm of x.

max([value1[, value2[, . . .]]])

Given zero or more arguments, converts each of the arguments to a number value and returns the largest of the resulting values.

min([value1[, value2[, . . .]]])

Given zero or more arguments, converts each of the arguments to a number value and returns the smallest of the resulting values.

pow(x, y)

Returns an approximation to the result of raising x to the power y.

random()

Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range.

round(x)

Returns the number value that is closest to x and is equal to a mathematical integer. If two integer number values are equally close to x, then the result is the number value that is closer to +infinity. If x is already an integer, the result is x.

Note: Math.round(3.5) returns 4, but Math.round(–3.5) returns –3.

sin(x)

Returns an approximation to the sine of x. The argument is expressed in radians.

sqrt(x)

Returns an approximation to the square root of x.

tan(x)

Returns an approximation to the tangent of x. The argument is expressed in radians.

Comment on this topic

Topic ID: 150102