## Math

The Math object holds a set of constants and methods enabling more complex mathematical operations than the basic arithmetic operators discussed in Chapter 4. You cannot instantiate a Math object as you would an Array or Date. The Math object is static (automatically created by the interpreter) so its properties are accessed directly. For example, to compute the square root of 10, the sqrt() method is accessed through the Math object directly:

`var root = Math.sqrt(10);`

Table 7-3 gives a complete list of constants provided by Math. A complete list of mathematical methods is given in Table 7-4.

Table 7-3: Constants Provided by the Math Object

Property

Description

Math.E

The base of the natural logarithm (Euler's constant e)

Math.LN2

Natural log of 2

Math.LN10

Natural log of 10

Math.LOG2E

Log (base 2) of e

Math.LOG10E

Log (base 10) of e

Math.PI

Pi (p)

Math.SQRT1_2

Square root of 0.5 (equivalently, one over the square root of 2)

Math.SQRT2

Square root of 2

Table 7-4: Methods Provided by the Math Object

Method

Returns

Math.abs(arg)

Absolute value of arg

Math.acos(arg)

Arc cosine of arg

Math.asin(arg)

Arc sine of arg

Math.atan(arg)

Arc tangent of arg

Math.atan2(y, x)

Angle between the x axis and the point (x, y), measured counterclockwise (like polar coordinates). Note how y is passed as the first argument rather than the second.

Math.ceil(arg)

Ceiling of arg (smallest integer greater than or equal to arg)

Math.cos(arg)

Cosine of arg

Math.exp(arg)

e to arg power

Math.floor(arg)

Floor of arg (greatest integer less than or equal to arg)

Math.log(arg)

Natural log of arg (log base e of arg)

Math.max(arg1, arg2)

The greater of arg1 or arg2

Math.min(arg1, arg2)

The lesser of arg1 or arg2

Math.pow(arg1, arg2)

arg1 to the arg2 power

Math.random()

A random number in the interval [0,1]

Math.round(arg)

The result of rounding arg to the nearest integer. If the decimal portion of arg is greater than or equal to .5, it is rounded up. Otherwise, arg is rounded down.

Math.sin(arg)

Sine of arg

Math.sqrt(arg)

Square root of arg

Math.tan(arg)

Tangent of arg

There are several aspects of the Math object that need to be kept in mind. The trigonometric methods work in radians, so you need to multiply any degree measurements by p / 180 before using them. Also, because of the imprecise characteristic of floating-point operations, you might notice minor deviations from the results you expect. For example, though the sine of p is 0, the following code:

`alert(Math.sin(Math.PI));`

gives the result

This value is very close to zero, but just large enough to trip up sensitive calculations.

It might seem that Math does not provide the capability to compute logarithms in bases other than e. Indeed it does not, directly. However, the following mathematical identity

`loga n = (loge n) / (loge a)`

can be used to compute logarithms in an arbitrary base. For example, you can compute the log base 2 of 64 as

`var x = Math.log(64) / Math.log(2);`

### Random Numbers

Because the Math.random() method returns values between zero and one, you must normalize its return value to fit the range of numbers required of your application. An easy way to get random integers in the range m to n (inclusive) is as follows:

`Math.round(Math.random() * (n - m)) + m;`

So to simulate a die roll you would use

`roll = Math.round(Math.random() * (6 - 1)) + 1;`

Generating random numbers in this manner is sufficient for most applications, but if “high quality” randomness is required, a more advanced technique should be used.

### Easing Math Computations

When working extensively with the Math object, it is often convenient to use the with statement. Doing so allows you to use Math properties without prefixing them with “Math.” The concept is illustrated by the following example (computing the length of a side of a triangle with the Law of Cosines):

``` with (Math)
{
var a = 3, b = 4, c;
var angleA = atan(a / b);
var angleB = atan(b / a);
var angleC = PI / 2;
c = pow(a, 2) + pow(b, 2) - 2 * a * b * cos(angleC);
c = sqrt(c);
}```