Math Object

The Math object is used for various forms of mathematical calculations. It contains several properties that are standard constants, such as pi = 3.14159…, as well as a large set of methods that represent common trigonometric and algebraic functions. All Math methods deal with floating-point numbers. Angles are expected to be given in radians, not degrees.

The Math object is our first example of a static object. A static object is one that does not change. All of the slots in the Math object already have values. This makes perfect sense, since you cannot change the value of pi or invent a new meaning for the cos() function (not without creating chaos). The practical consequence of Math being static is that you never use new with Math; you always refer to the Math object directly. Math is the opposite of the String object. The String object has instances but no explicit object; the Math object has only itself, and no instances.

Math Object Properties

PROPERTY NAME EXAMPLE RETURNED VALUE
E
(2.718281828459045091)
Math.E * 5 13.59140914229522501
LN10
(2.302585092994045901)
Math.LN10 / 6 0.3837641821656743168
LN2
(0.69314718055994529)
Math.LN2 - Math.E -2.025134647899099694
PI
(3.141592653589793116)
Math.sin(2 * Math.PI / 4) 0.2741213359
SQRT2
(1.414213562)
1 / Math.SQRT2 0.7071067811865474617

Math Object Methods

METHOD NAME EXAMPLE RETURNED VALUE
abs Math.abs(-6.5) 6.5
acos Math.acos(.5) 1.047197551196597631
asin Math.asin(1) 1.570796326794896558
atan Math.atan(.5) 0.4636476090008060935
ceil Math.ceil(7.6) 8
cos Math.cos(.4) 0.9210609940028851028
exp Math.exp(8) 2980.957987041728302
floor Math.floor(8.9) 8
log Math.log(5) 1.609437912434100282
max Math.max(1, 700) 700
min Math.min(1, 700) 1
pow Math.pow(6, 2) 36
random Math.random() .7877896
round Math.round(.567) 1
sin Math.sin(Math.PI) 0
sqrt Math.sqrt(9801) 99
tan Math.tan(1.5 * Math.PI) INF (infinity)