Chapter 15
JavaScript Math
Math in JavaScript
Computers are well known for their ability to perform complex calculations at extreme
speeds. All programming languages include features, constants, functions, and other math
elements that unleash the mathematical power of the computer. Likewise, JavaScript
includes many features related to math, enabling JavaScript scripters to invoke numeric
calculations in our scripts.
Math in JavaScript is based on two general elements of the language:
- Mathematical operators
- The built-in Math object
Mathematical operators should be clear by now, because they were discussed in Chapter
7, Utilizing JavaScript Operators. However, you are not yet familiar with the Math
object. In this chapter we shall take a look into this object, including its properties
and methods. We shall also take a look at some possible usages of this object.
The Math Object
JavaScript’s Math object provides many arithmetic and trigonometric
functions. Most of these are functions you probably already know. These functions expand
the mathematical ability of JavaScript beyond the basic arithmetic operators.
Although JavaScript does not feature classes as Java does, built-in objects resemble
classes in many ways. You already know how to create instances of dynamic objects, such as
Date. Instances are created according to dynamic objects. Some objects in
JavaScript are static ones. That is, they act like a single instance. You can refer
directly to the object’s methods and properties. This is different from dynamic objects
such as Date. Recall that to retrieve the current year, you must first create an
instance of the object, and then refer to the instance’s methods.
JavaScript’s Math object is a static one. Its properties are actually basic
constants, such as PI ([pi]) and the square root of 2. Its methods are mathematical
functions, such as pow() (power), sin(), cos(), and others.
These methods and properties are encapsulated in an object because objects are meant to be
entities constructed of related data and functions—a good definition of math. As a
general rule, JavaScript prefers to organize its functions and constants in its structured
object model, to enable simple reference and maintenance. The requirements from
mathematical functions and constants led JavaScript’s developers to create a built-in
object related to mathematics—Math.
To access elements of the Math object, you do not need to create an instance.
You access them via messages sent directly by the Math object itself. For
example, the PI constant is a property of the Math object, and can be accessed
via the following syntax:
var pi = Math.PI
The reason that the Math object is static is that mathematical constants and
functions never change. For example, the value of PI will not change, nor will any
trigonometric function.
The Math object implementation is inspired by the Math class in Java.
Despite this parallelism, you will find that JavaScript offers fewer methods.
Constants
Constants are defined with the full precision of floating point numbers in JavaScript.
All constants in JavaScript are properties of the Math object. In this section we
shall outline these properties for your reference and understanding. Notice that all
properties are specified with capital letters, although this convention is not common in
the mathematical community. All properties refer to well-known constants (read your math
books if you don’t remember!); that is, they do not change. Therefore, these properties
are read-only ones, and accessing them for the purpose of modification results in a
JavaScript error.
E
A very important constant in mathematics is Euler’s constant. Its approximate value
is 2.718281828459045. It is rounded off to 15 digits after the decimal point. For your
reference, the equation is:
ei[pi] + 1 = 0
In JavaScript you refer to it via a capital E; that is, Math.E.
LN2
Another constant featured as a property of the Math object is the natural
logarithm of 2. Its approximate value is 0.6931471805599453. A defining equation is:
eLN2 = 2
JavaScript refers to this number as LN2. Because it is a property of the Math
object, you should specify them together as in Math.LN2.
You can use the pow method to assure that the preceding equation is true:
document.write(Math.pow(Math.E, Math.LN2))
Because both Euler’s constant and the natural logarithm of 2 are approximate, the
output of this statement is also an approximate:
1.9999999999999998
LN10
The natural logarithm of 10 is also featured as a property of the static Math
object. Its value, as stored in its corresponding property, is 2.302585092994046. Once
again, you can understand this value via an equation:
eLN10 = 10
In JavaScript this value is referred to as Math.LN10.
Once more, here is a JavaScript statement to define the natural logarithm of 10:
document.write(Math.pow(Math.E, Math.LN10))
Since both Euler’s constant and the natural logarithm of 10 are approximate, the
output of this statement is also an approximate:
10.000000000000002
LOG2E
Another important constant in the math arena is the base-2 logarithm of Euler’s
constant. Its approximate value is 1.4426950408889634. In math that is:
2LOG2E = e
As you can see, you refer to this constant in JavaScript as Math.LOG2E. Here
is a simple statement to confirm the value:
document.write(Math.pow(2, Math.LOG2E) – Math.E)
This time the output is apparently exact:
0
LOG10E
The base-10 logarithm is also widely used in complex mathematical calculations. Its
value in JavaScript is approximately 0.4342944819032518. The following equation
demonstrates the definition of the constant:
10LOG10E = e
As you can see, the equation is built according to one of the basic logarithm rules. In
JavaScript, log base-10 of Euler’s constant is a property of the Math object: Math.LOG10E.
Here is a simple script for confirmation:
document.write(Math.pow(10, Math.LOG10E) – Math.E)
Once again, the output is exact:
0