#### 3.1.3 Arithmetic in JavaScript

guage provides. These include + for addition, - for subtraction, * for multiplica-

tion, / for division, and % for modulo (remainder after division). Full details on these

and other operators can be found in Chapter 4.

In addition to these basic arithmetic operators, JavaScript supports more complex

mathematical operations through a set of functions and constants defined as properties

of the Math object:

Math.pow(2,53) // => 9007199254740992: 2 to the power 53

Math.round(.6) // => 1.0: round to the nearest integer

Math.ceil(.6) // => 1.0: round up to an integer

Math.floor(.6) // => 0.0: round down to an integer

Math.abs(-5) // => 5: absolute value

Math.max(x,y,z) // Return the largest argument

Math.min(x,y,z) // Return the smallest argument

Math.random() // Pseudo-random number x where 0 <= x < 1.0

Math.PI // π: circumference of a circle / diameter

Math.E // e: The base of the natural logarithm

Math.sqrt(3) // The square root of 3

Math.pow(3, 1/3) // The cube root of 3

Math.sin(0) // Trigonometry: also Math.cos, Math.atan, etc.

Math.log(10) // Natural logarithm of 10

Math.log(100)/Math.LN10 // Base 10 logarithm of 100

Math.log(512)/Math.LN2 // Base 2 logarithm of 512

Math.exp(3) // Math.E cubed

See the Math object in the reference section for complete details on all the mathematical

functions supported by JavaScript.

Arithmetic in JavaScript does not raise errors in cases of overflow, underflow, or divi-

sion by zero. When the result of a numeric operation is larger than the largest repre-

sentable number (overflow), the result is a special infinity value, which JavaScript prints

as Infinity. Similarly, when a negative value becomes larger than the largest repre-

sentable negative number, the result is negative infinity, printed as -Infinity. The in-

finite values behave as you would expect: adding, subtracting, multiplying, or dividing

them by anything results in an infinite value (possibly with the sign reversed).

Underflow occurs when the result of a numeric operation is closer to zero than the

smallest representable number. In this case, JavaScript returns 0. If underflow occurs

from a negative number, JavaScript returns a special value known as “negative zero.”

This value is almost completely indistinguishable from regular zero and JavaScript

programmers rarely need to detect it.

Division by zero is not an error in JavaScript: it simply returns infinity or negative

infinity. There is one exception, however: zero divided by zero does not have a well-

defined value, and the result of this operation is the special not-a-number value, printed

as NaN. NaN also arises if you attempt to divide infinity by infinity, or take the square

root of a negative number or use arithmetic operators with non-numeric operands that

cannot be converted to numbers.

JavaScript predefines global variables Infinity and NaN to hold the positive infinity and

not-a-number value. In ECMAScript 3, these are read/write values and can be changed.

ECMAScript 5 corrects this and makes the values read-only. The Number object defines

alternatives that are read-only even in ECMAScript 3. Here are some examples:

Infinity // A read/write variable initialized to Infinity.

Number.POSITIVE_INFINITY // Same value, read-only.

1/0 // This is also the same value.

Number.MAX_VALUE + 1 // This also evaluates to Infinity.

Number.NEGATIVE_INFINITY // These expressions are negative infinity.

-Infinity

-1/0

-Number.MAX_VALUE - 1

NaN // A read/write variable initialized to NaN.

Number.NaN // A read-only property holding the same value.

0/0 // Evaluates to NaN.

Number.MIN_VALUE/2 // Underflow: evaluates to 0

-Number.MIN_VALUE/2 // Negative zero

-1/Infinity // Also negative 0

-0

The not-a-number value has one unusual feature in JavaScript: it does not compare

equal to any other value, including itself. This means that you can’t write x == NaN to

determine whether the value of a variable x is NaN. Instead, you should write x != x.

That expression will be true if, and only if, x is NaN. The function isNaN() is similar. It

returns true if its argument is NaN, or if that argument is a non-numeric value such as

a string or an object. The related function isFinite() returns true if its argument is a

number other than NaN, Infinity, or -Infinity.

The negative zero value is also somewhat unusual. It compares equal (even using Java-

Script’s strict equality test) to positive zero, which means that the two values are almost

indistinguishable, except when used as a divisor:

var zero = 0; // Regular zero

var negz = -0; // Negative zero

zero === negz // => true: zero and negative zero are equal

1/zero === 1/negz // => false: infinity and -infinity are not equal