Math object
Browser support:
The Math object allows the use of mathematical constants (such as pi) and functions (such as sine).
Syntax:
Using
Math properties, such as pi:
var pi = Math.PI;
Using
Math methods, such as sin:
Math.sin (x );
Members:
The
Math object inherited from the
Object .prototype object.
The following lists only contain the members of the
Math object.
Properties:
E
Returns Euler's number (e, the base of the natural logarithm). Numerical value truncated to 4 decimal places: 2.7182.
LN2
Natural logarithm of 2. Numerical value truncated to 4 decimal places: 0.6931.
LN10
Natural logarithm of 10. Numerical value truncated to 4 decimal places: 2.3025.
LOG2E
Binary logarithm (logarithm for base 2) of Euler's number. Numerical value truncated to 4 decimal places: 1.4426.
LOG10E
Common logarithm (logarithm with base 10) of Euler's number. Numerical value truncated to 4 decimal places: 0.4342.
PI
Represents the ratio of any circle's circumference to its diameter in Euclidean geometry. 3.14159.
SQRT1_2
Square root of 1/2. Numerical value truncated to 4 decimal places: 0.7071.
SQRT2
The square root of 2, also known as Pythagoras' constant. Numerical value truncated to 4 decimal places: 1.4142.
Methods:
abs (number )
Returns the absolute value of a floating-point number. In other words, returns the unsigned value of a real number.
number
Required. Specifies a floating-point number.
acos (number )
Returns the arc cosine of the specified number, in radians.
number
Required. Specifies a floating point number from -1 to 1.
asin (number )
Returns the arc sine of the specified number, in radians.
number
Required. Specifies a floating point number from -1 to 1.
atan (number )
Returns the arc tangent of the specified number, in radians.
number
Required. Specifies a floating point number.
atan2 (Y, X )
Returns the direction (angle) of the (X, Y) position vector (vector from origin to (X, Y)), in radians.
Equivalent to atan (Y / X).
Y
Required. The y-coordinate of the point.
X
Required. The x-coordinate of the point.
ceil (number )
Returns the smallest integer that is greater than or equal to the given number.
number
Required. Specifies a floating point number.
Example 2 shows different methods for converting floating point values to integers.
cos (angle )
Returns the cosine of the specified angle.
angle
Required. A floating point number that specifies an angle, in radians.
exp (x )
Returns the value of ex , where e is Euler's number and x is the power.
x
Required. Specifies a floating point number.
floor (number )
Returns the greatest integer that is smaller than or equal to the given number.
number
Required. Specifies a floating point number.
Example 2 shows different methods for converting floating point values to integers.
log (number )
Returns the natural logarithm of the given number. Returns the power to which the base e (Euler's number) must be raised to obtain the given number.
number
Required. Specifies a floating point number.
max ([number1 [, number2 [. . . [, numberN]]]] )
Returns the greatest value from the specified numbers.
[number1 [, number2 [. . . [, numberN]]]]
Required. Numbers to compare.
min ([number1 [, number2 [. . . [, numberN]]]] )
Returns the smallest value from the specified numbers.
[number1 [, number2 [. . . [, numberN]]]]
Required. Numbers to compare.
pow (base, exponent )
Returns the value of baseexponent , the base to the exponent power.
base
Required. The base number.
exponent
Required. The exponent.
random ()
Reruns a random number between 0 and 1.
round (number )
Returns the nearest integer for the given number.
number
Required. Specifies a floating point number.
Example 2 shows different methods for converting floating point values to integers.
sin (angle )
Returns the sine of the specified angle.
angle
Required. A floating point number that specifies an angle, in radians.
sqrt (number )
Returns the square root of the given number.
number
Required. Specifies a floating point number.
tan (angle )
Returns the tangent of the specified angle.
angle
Required. A floating point number that specifies an angle, in radians.
Examples:
Example 1:
How to get the value of pi:
var pi = Math . PI ;
document . write (pi); // output: 3.141592653589793
Did you find this example helpful?
yes
no
Example 2:
Converting floating point values to integers:
< head >
< script type = "text/javascript" >
function Init () {
var tbody = document . getElementById ( "tableBody" );
var values = [ 6.34 , 6.94 , - 6.34 , - 6.94 ];
for ( var i = 0 ; i < values. length ; i++) {
var row = tbody. insertRow (- 1 );
var cell = row. insertCell (- 1 );
cell. innerHTML = values[i];
var cell = row. insertCell (- 1 );
cell. innerHTML = Math . round (values[i]);
var cell = row. insertCell (- 1 );
cell. innerHTML = Math . floor (values[i]);
var cell = row. insertCell (- 1 );
cell. innerHTML = Math . ceil (values[i]);
var cell = row. insertCell (- 1 );
cell. innerHTML = ~~values[i];
var cell = row. insertCell (- 1 );
cell. innerHTML = (values[i] ^ 0 );
var cell = row. insertCell (- 1 );
cell. innerHTML = (values[i] << 0 );
}
}
</ script >
</ head >
< body onload = "Init ()" >
< table border = "1px" cellspacing = "0" cellpadding = "5px" >
< thead >
< tr >
< th > x </ th >
< th > Math.round (x) </ th >
< th > Math.floor (x) </ th >
< th > Math.ceil (x) </ th >
< th > ~~x </ th >
< th > x ^ 0 </ th >
< th > x << 0 </ th >
</ tr >
</ thead >
< tbody id = "tableBody" >
</ tbody >
</ table >
</ body >
Did you find this example helpful?
yes
no
Example 3:
How to get a number's exponent:
var exp = Math . pow ( 2 , 3 );
document . write (exp); // output: 8
Did you find this example helpful?
yes
no
Example 4:
How to generate a random number:
var random = Math . random ();
document . write (random); // output: a number between 0 and 1
Did you find this example helpful?
yes
no
External links:
Share:
Digg
Del.icio.us
Reddit
Facebook
Twitter
Diigo
User Contributed Comments