OBJECT:  Math


The Math object is a top-level, built-in JavaScript object which can be accessed without using a constructor or calling a method. It also has static properties and methods for mathematical constants and functions. This means that you can refer to, say, the constant PI as Math.PI, and you can call the Tangent function with Math.tan(x). To illustrate this, the following code calculates the length of the side of a right-angled triangle opposite the angle Theta:
 
Code:
len = Math.tan(theta) * adj
 
All constants are defined as precision real numbers in JavaScript. When using several Math constants and methods, it is often more convenient to use the with statement to avoid having to repeatedly type the word Math:
 
Code:
with(Math)
{
   a = 28.27
   adj = sqrt(a/PI)
   len = adj * tan(1.1071)
}

 
PROPERTIES
 
E Property
This property is Euler's constant and the base of natural logarithms (approximately 2.7183)
 
Syntax: Math.E
 
LN10 Property
This property is the natural logarithm of 10, (approximately 2.3026).
 
Syntax: Math.LN10
 
LN2 Property
This property is the natural logarithm of 2, which is approximately 0.6931.
 
Syntax: Math.LN2
 
LOG10E Property
This property is the base 10 logarithm of E (approximately 0.4343).
 
Syntax: Math.LOG10E
 
LOG2E Property
This property is the base 2 logarithm of E (approximately 1.4427).
 
Syntax: Math.LOG2E
 
PI Property
This property is the ratio of the circuference of a circle to its diameter (approximately 3.1416).
 
Syntax: Math.PI
 
SQRT1_2 Property
This property is the value of 1 divided by the square root of 2 and is approximately equal to 0.7071.
 
Syntax: Math.SQRT1_2
 
SQRT2 Property
This property is the square root of 2 (approximately 1.4142).
 
Syntax: Math.SQRT2
 
METHODS
 
abs Method
This method returns the absolute value of a number.
 
Syntax: Math.abs(x)
 
acos Method
This method returns the arccosine of a number as a numeric value between 0 and PI radians. Passing it a value for 'x' which is outsite the range -1 to 1 will cause the Netscape browser to return NaN, and the Internet Explorer browser to return an error message. Passing it -1 will return the value of PI.
 
Syntax: Math.acos(x)
 
asin Method
This method returns the arcsine of a number as a numeric value between
-PI/2 and PI/2 radians. Passing it a value for 'x' which is outsite the range -1 to 1 will cause the Netscape browser to return NaN, and the Internet Explorer browser to return an error message. Passing it 1 will return the value of PI/2.
 
Syntax: Math.asin(x)
 
atan Method
This method returns the arctangent of a number as a numeric value between -PI/2 and PI/2 radians.
 
Syntax: Math.atan(x)
 
atan2 Method
This method returns the arctangent of the quotient of its arguments.
 
Syntax: Math.atan2(y, x)
 
ceil Method
This method returns an integer equal to, or the next integer greater than, the number passed to it. Hence, if you passed it 3.79, it would return 4, and passing it -3.79 would return -3.
 
Syntax: Math.ceil(x)
 
cos Method
This method returns the cosine of a number, which will be a numeric value between -1 and 1.
 
Syntax: Math.cos(x)
 
exp Method
This method returns the value of Ex where E is Euler's constant and x is the argument passed to it.
 
Syntax: Math.exp(x)
 
floor Method
This method returns an integer equal to, or the next integer less than, the number passed to it. Hence, if you passed it 3.79, it would return 3, and passing it -3.79 would return -4.
 
Syntax: Math.floor(x)
 
log Method
This method returns the natural logarithm (base E) of a number. If you pass the log method the number 0, the Netscape browser will return -Infinity, and with an argument of a negative number NaN. In both these cases Internet Explorer returns an error message.
 
Syntax: Math.log(x)
 
max Method
This method returns the greater of the two numbers passed to it as arguments. Hence, if you passed it the numbers 9 and 11, it would return 11, whereas passing it -9 and -11 returns -9.
 
Syntax: Math.max(x, y)
 
min Method
This method returns the lesser of the two numbers passed to it as arguments. Hence, if you passed it the numbers 9 and 11, it would return 9, whereas passing it -9 and -11 returns -11.
 
Syntax: Math.min(x, y)
 
pow Method
This method returns the value of x to the power of y (xy), where x is the base, and y is the exponent.
 
Syntax: Math.pow(x, y)
 
random Method
This method takes no arguments and returns a pseudo-random number between 0 and 1. The random number generator is seeded from the current time.
 
Syntax: Math.random()
 
round Method
This method is used to round a number to the nearest integer. If the fractional portion of the number is .5 or higher, then the number is rounded up, otherwise it is rounded down.
 
Syntax: Math.round(x)
 
sin Method
This method is used to return the sine of its argument, which will be a number between -1 and 1.
 
Syntax: Math.sin(x)
 
sqrt Method
This method returns the square root of a number. If that number is negative, then the Netscape browser returns the value of NaN, whereas the Internet Explorer browser returns an Error message.
 
Syntax: Math.sqrt(x)
 
tan Method
This method returns a number representing the tangent of an angle.
 
Syntax: Math.tan(x)


Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information