The Math Object


The Math Object

The Math object gives you access to powerful math routines, such as log , sqrt , and pow (which raises values to various powers). We've already seen the Math object in the book every now and then when we had to round off numbers or find absolute values. You can see the properties (actually, they're read-only constants, which is why they're all in capital letters ) and methods of the Math object in Table 19.1, its properties in depth in Table 19.2, and its methods in Table 19.3.

Tip

The trig methods of the Math object take or return radian values. To convert an angle in degrees to radians, multiply that angle by pi and divide by 180.


Table 19.1. Overview of the Properties and Methods of the Math Object

Properties

Methods

E

abs

LN2

acos

LN10

asin

LOG2E

atan

LOG10E

atan2

PI

ceil

SQRT1_2

cos

SQRT2

exp

 

floor

 

log

 

max

 

min

 

pow

 

random

 

round

 

sin

 

sqrt

 

tan

Table 19.2. The Properties of the Math Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

E

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

The natural log constant e, stored in JavaScript as 2.718281828459045091.

LN2

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

The natural log of 2, stored in JavaScript as 0.6931471805599452862.

LN10

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

The natural log of 10, stored in JavaScript as 2.302585092994045901.

LOG2E

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

The base 2 log of e, stored in JavaScript as 1.442695040888963387.

LOG10E

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

The base 10 log of e, stored in JavaScript as 0.4342944819032518167.

PI

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

Pi, stored in JavaScript as 3.141592653589793116.

SQRT1_2

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

The square root of one half, stored in JavaScript as 0.7071067811865475727.

SQRT2

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

The square root of 2, stored in JavaScript as 1.414213562373095145.

Table 19.3. The Methods of the Math Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

Eabs

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the absolute value of a number.

 

Syntax: Math.abs( value ) , where value is the number for which you want the absolute value.

acos

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the arc cosine of a value, in radians.

 

Syntax: Math.acos( value ) , where value is the value for which you want the arc cosine.

asin

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the arc sine of a value, in radians.

 

Syntax: Math.asin( value ) , where value is the value for which you want the arc sine.

atan

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the arc tangent of a value, in radians.

 

Syntax: Math.atan( value ) , where value is the value for which you want the arc tangent.

atan2

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the angle (in radians) from the X axis to a point ( y , x ).

 

Syntax: Math.atan2( y , x ) .

ceil

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the smallest integer greater than or equal to the value you pass.

 

Syntax: Math.ceil( value ) , where value is the value you want to round up.

cos

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the cosine of a value.

 

Syntax: Math.cos( value ) , where value is the value, in radians, for which you want the cosine.

exp

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns e to the power you specify.

 

Syntax: Math.exp( value ) , where value is the value for which you want e to the power.

floor

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the greatest integer less than or equal to the value you pass.

 

Syntax: Math.floor( value ) , where value is the value you want to round down.

log

x

x

x

x

x

x

x

x

x

x

 

Returns: number

 

Returns the natural log, base e, of a value.

 

Syntax: Math.log( value ) , where value is the value for which you want the natural log.

max

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the greater of the two values you pass to it.

 

Syntax: Math.max( value1 , value2 ) , where value1 and value2 are the values you're comparing.

min

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the lesser of the two values you pass to it.

 

Syntax: Math.min( value1 , value2 ) , where value1 and value2 are the values you're comparing.

pow

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the value of a base expression raised to a given power.

 

Syntax: Math.pow( base , exponent ) .

random

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns a random number between 0 and 1.

 

Syntax: Math.random() .

round

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns an numeric expression rounded to the nearest integer.

 

Math.round( value ) . If the decimal part of value is 0.5 or greater, the return value is the smallest integer that is still greater than value . Otherwise, the return value is the largest integer less than or equal to value .

sin

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the sine of a value.

 

Syntax: Math.sin( value ) , where value is the value, in radians, for which you want the sine.

sqrt

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the square root of a value.

 

Syntax: Math.sqrt( value ) , where value is the value, in radians, for which you want the square root.

tan

x

x

x

x

x

x

x

x

x

x

 

Returns: Number

 

Returns the tangent of a value.

 

Syntax: Math.tan( value ) , where value is the value, in radians, for which you want the tangent.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net