21.3 The Math Object
The Math object:
- is %Math%.
- is the initial value of the
"Math" property of theglobal object . - is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is not a
function object . - does not have a [[Construct]] internal method; it cannot be used as a
constructor with thenew
operator. - does not have a [[Call]] internal method; it cannot be invoked as a function.
In this specification, the phrase “the
21.3.1 Value Properties of the Math Object
21.3.1.1 Math.E
The
This property has the attributes { [[Writable]]:
21.3.1.2 Math.LN10
The
This property has the attributes { [[Writable]]:
21.3.1.3 Math.LN2
The
This property has the attributes { [[Writable]]:
21.3.1.4 Math.LOG10E
The
This property has the attributes { [[Writable]]:
The value of Math.LOG10E
is approximately the reciprocal of the value of Math.LN10
.
21.3.1.5 Math.LOG2E
The
This property has the attributes { [[Writable]]:
The value of Math.LOG2E
is approximately the reciprocal of the value of Math.LN2
.
21.3.1.6 Math.PI
The
This property has the attributes { [[Writable]]:
21.3.1.7 Math.SQRT1_2
The
This property has the attributes { [[Writable]]:
The value of Math.SQRT1_2
is approximately the reciprocal of the value of Math.SQRT2
.
21.3.1.8 Math.SQRT2
The
This property has the attributes { [[Writable]]:
21.3.1.9 Math [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]:
21.3.2 Function Properties of the Math Object
The behaviour of the functions acos
, acosh
, asin
, asinh
, atan
, atanh
, atan2
, cbrt
, cos
, cosh
, exp
, expm1
, hypot
, log
, log1p
, log2
, log10
, pow
, random
, sin
, sinh
, sqrt
, tan
, and tanh
is not precisely specified here except to require specific results for certain argument values that represent boundary cases of interest. For other argument values, these functions are intended to compute approximations to the results of familiar mathematical functions, but some latitude is allowed in the choice of approximation algorithms. The general intent is that an implementer should be able to use the same mathematical library for ECMAScript on a given hardware platform that is available to C programmers on that platform.
Although the choice of algorithms is left to the implementation, it is recommended (but not specified by this standard) that implementations use the approximation algorithms for fdlibm
, the freely distributable mathematical library from Sun Microsystems (http://www.netlib.org/fdlibm).
21.3.2.1 Math.abs ( x )
This function returns the absolute value of x; the result has the same magnitude as x but has positive sign.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , returnNaN . - If n is
-0 𝔽, return+0 𝔽. - If n is
-∞ 𝔽, return+∞ 𝔽. - If n <
-0 𝔽, return -n. - Return n.
21.3.2.2 Math.acos ( x )
This function returns the inverse cosine of x. The result is expressed in radians and is in the
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , n >1 𝔽, or n <-1 𝔽, returnNaN . - If n is
1 𝔽, return+0 𝔽. - Return an
implementation-approximated Number value representing the result of the inverse cosine ofℝ (n).
21.3.2.3 Math.acosh ( x )
This function returns the inverse hyperbolic cosine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n <
1 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the inverse hyperbolic cosine ofℝ (n).
21.3.2.4 Math.asin ( x )
This function returns the inverse sine of x. The result is expressed in radians and is in the
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n >
1 𝔽 or n <-1 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the inverse sine ofℝ (n).
21.3.2.5 Math.asinh ( x )
This function returns the inverse hyperbolic sine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - Return an
implementation-approximated Number value representing the result of the inverse hyperbolic sine ofℝ (n).
21.3.2.6 Math.atan ( x )
This function returns the inverse tangent of x. The result is expressed in radians and is in the
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is
+∞ 𝔽, return animplementation-approximated Number value representing π / 2. - If n is
-∞ 𝔽, return animplementation-approximated Number value representing -π / 2. - Return an
implementation-approximated Number value representing the result of the inverse tangent ofℝ (n).
21.3.2.7 Math.atanh ( x )
This function returns the inverse hyperbolic tangent of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n >
1 𝔽 or n <-1 𝔽, returnNaN . - If n is
1 𝔽, return+∞ 𝔽. - If n is
-1 𝔽, return-∞ 𝔽. - Return an
implementation-approximated Number value representing the result of the inverse hyperbolic tangent ofℝ (n).
21.3.2.8 Math.atan2 ( y, x )
This function returns the inverse tangent of the quotient
It performs the following steps when called:
- Let ny be ?
ToNumber (y). - Let nx be ?
ToNumber (x). - If ny is
NaN or nx isNaN , returnNaN . - If ny is
+∞ 𝔽, then- If nx is
+∞ 𝔽, return animplementation-approximated Number value representing π / 4. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing 3π / 4. - Return an
implementation-approximated Number value representing π / 2.
- If nx is
- If ny is
-∞ 𝔽, then- If nx is
+∞ 𝔽, return animplementation-approximated Number value representing -π / 4. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing -3π / 4. - Return an
implementation-approximated Number value representing -π / 2.
- If nx is
- If ny is
+0 𝔽, then- If nx >
+0 𝔽 or nx is+0 𝔽, return+0 𝔽. - Return an
implementation-approximated Number value representing π.
- If nx >
- If ny is
-0 𝔽, then- If nx >
+0 𝔽 or nx is+0 𝔽, return-0 𝔽. - Return an
implementation-approximated Number value representing -π.
- If nx >
Assert : ny isfinite and is neither+0 𝔽 nor-0 𝔽.- If ny >
+0 𝔽, then- If nx is
+∞ 𝔽, return+0 𝔽. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing π. - If nx is either
+0 𝔽 or-0 𝔽, return animplementation-approximated Number value representing π / 2.
- If nx is
- If ny <
-0 𝔽, then- If nx is
+∞ 𝔽, return-0 𝔽. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing -π. - If nx is either
+0 𝔽 or-0 𝔽, return animplementation-approximated Number value representing -π / 2.
- If nx is
Assert : nx isfinite and is neither+0 𝔽 nor-0 𝔽.- Let r be the inverse tangent of
abs (ℝ (ny) /ℝ (nx)). - If nx <
-0 𝔽, then- If ny >
+0 𝔽, set r to π - r. - Else, set r to -π + r.
- If ny >
- Else,
- If ny <
-0 𝔽, set r to -r.
- If ny <
- Return an
implementation-approximated Number value representing r.
21.3.2.9 Math.cbrt ( x )
This function returns the cube root of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - Return an
implementation-approximated Number value representing the result of the cube root ofℝ (n).
21.3.2.10 Math.ceil ( x )
This function returns the smallest (closest to -∞)
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - If n <
-0 𝔽 and n >-1 𝔽, return-0 𝔽. - If n is an
integral Number , return n. - Return the smallest (closest to -∞)
integral Number value that is not less than n.
The value of Math.ceil(x)
is the same as the value of -Math.floor(-x)
.
21.3.2.11 Math.clz32 ( x )
This function performs the following steps when called:
If n is either
21.3.2.12 Math.cos ( x )
This function returns the cosine of x. The argument is expressed in radians.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite , returnNaN . - If n is either
+0 𝔽 or-0 𝔽, return1 𝔽. - Return an
implementation-approximated Number value representing the result of the cosine ofℝ (n).
21.3.2.13 Math.cosh ( x )
This function returns the hyperbolic cosine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , returnNaN . - If n is either
+∞ 𝔽 or-∞ 𝔽, return+∞ 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return1 𝔽. - Return an
implementation-approximated Number value representing the result of the hyperbolic cosine ofℝ (n).
The value of Math.cosh(x)
is the same as the value of (Math.exp(x) + Math.exp(-x)) / 2
.
21.3.2.14 Math.exp ( x )
This function returns the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is either
+0 𝔽 or-0 𝔽, return1 𝔽. - If n is
-∞ 𝔽, return+0 𝔽. - Return an
implementation-approximated Number value representing the result of the exponential function ofℝ (n).
21.3.2.15 Math.expm1 ( x )
This function returns the result of subtracting 1 from the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms). The result is computed in a way that is accurate even when the value of x is close to 0.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽,-0 𝔽, or+∞ 𝔽, return n. - If n is
-∞ 𝔽, return-1 𝔽. - Return an
implementation-approximated Number value representing the result of subtracting 1 from the exponential function ofℝ (n).
21.3.2.16 Math.floor ( x )
This function returns the greatest (closest to +∞)
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - If n <
1 𝔽 and n >+0 𝔽, return+0 𝔽. - If n is an
integral Number , return n. - Return the greatest (closest to +∞)
integral Number value that is not greater than n.
The value of Math.floor(x)
is the same as the value of -Math.ceil(-x)
.
21.3.2.17 Math.fround ( x )
This function performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , returnNaN . - If n is one of
+0 𝔽,-0 𝔽,+∞ 𝔽, or-∞ 𝔽, return n. - Let n32 be the result of converting n to
IEEE 754-2019 binary32 format using roundTiesToEven mode. - Let n64 be the result of converting n32 to
IEEE 754-2019 binary64 format. - Return the ECMAScript Number value corresponding to n64.
21.3.2.18 Math.hypot ( ...args )
Given zero or more arguments, this function returns the square root of the sum of squares of its arguments.
It performs the following steps when called:
- Let coerced be a new empty
List . - For each element arg of args, do
- Let n be ?
ToNumber (arg). - Append n to coerced.
- Let n be ?
- For each element number of coerced, do
- If number is either
+∞ 𝔽 or-∞ 𝔽, return+∞ 𝔽.
- If number is either
- Let onlyZero be
true . - For each element number of coerced, do
- If number is
NaN , returnNaN . - If number is neither
+0 𝔽 nor-0 𝔽, set onlyZero tofalse .
- If number is
- If onlyZero is
true , return+0 𝔽. - Return an
implementation-approximated Number value representing the square root of the sum of squares of themathematical values of the elements of coerced.
The
Implementations should take care to avoid the loss of precision from overflows and underflows that are prone to occur in naive implementations when this function is called with two or more arguments.
21.3.2.19 Math.imul ( x, y )
This function performs the following steps when called:
21.3.2.20 Math.log ( x )
This function returns the natural logarithm of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return-∞ 𝔽. - If n <
-0 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the natural logarithm ofℝ (n).
21.3.2.21 Math.log1p ( x )
This function returns the natural logarithm of 1 + x. The result is computed in a way that is accurate even when the value of x is close to zero.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽,-0 𝔽, or+∞ 𝔽, return n. - If n is
-1 𝔽, return-∞ 𝔽. - If n <
-1 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the natural logarithm of 1 +ℝ (n).
21.3.2.22 Math.log10 ( x )
This function returns the base 10 logarithm of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return-∞ 𝔽. - If n <
-0 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the base 10 logarithm ofℝ (n).
21.3.2.23 Math.log2 ( x )
This function returns the base 2 logarithm of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return-∞ 𝔽. - If n <
-0 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the base 2 logarithm ofℝ (n).
21.3.2.24 Math.max ( ...args )
Given zero or more arguments, this function calls
It performs the following steps when called:
- Let coerced be a new empty
List . - For each element arg of args, do
- Let n be ?
ToNumber (arg). - Append n to coerced.
- Let n be ?
- Let highest be
-∞ 𝔽. - For each element number of coerced, do
- If number is
NaN , returnNaN . - If number is
+0 𝔽 and highest is-0 𝔽, set highest to+0 𝔽. - If number > highest, set highest to number.
- If number is
- Return highest.
The comparison of values to determine the largest value is done using the
The
21.3.2.25 Math.min ( ...args )
Given zero or more arguments, this function calls
It performs the following steps when called:
- Let coerced be a new empty
List . - For each element arg of args, do
- Let n be ?
ToNumber (arg). - Append n to coerced.
- Let n be ?
- Let lowest be
+∞ 𝔽. - For each element number of coerced, do
- If number is
NaN , returnNaN . - If number is
-0 𝔽 and lowest is+0 𝔽, set lowest to-0 𝔽. - If number < lowest, set lowest to number.
- If number is
- Return lowest.
The comparison of values to determine the largest value is done using the
The
21.3.2.26 Math.pow ( base, exponent )
This function performs the following steps when called:
- Set base to ?
ToNumber (base). - Set exponent to ?
ToNumber (exponent). - Return
Number::exponentiate (base, exponent).
21.3.2.27 Math.random ( )
This function returns a Number value with positive sign, greater than or equal to
Each Math.random
function created for distinct
21.3.2.28 Math.round ( x )
This function returns the Number value that is closest to x and is integral. If two
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is anintegral Number , return n. - If n <
0.5 𝔽 and n >+0 𝔽, return+0 𝔽. - If n <
-0 𝔽 and n ≥-0.5 𝔽, return-0 𝔽. - Return the
integral Number closest to n, preferring the Number closer to +∞ in the case of a tie.
Math.round(3.5)
returns 4, but Math.round(-3.5)
returns -3.
The value of Math.round(x)
is not always the same as the value of Math.floor(x + 0.5)
. When x
is x
is less than Math.round(x)
returns Math.floor(x + 0.5)
returns Math.round(x)
may also differ from the value of Math.floor(x + 0.5)
because of internal rounding when computing x + 0.5
.
21.3.2.29 Math.sign ( x )
This function returns the sign of x, indicating whether x is positive, negative, or zero.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n <
-0 𝔽, return-1 𝔽. - Return
1 𝔽.
21.3.2.30 Math.sin ( x )
This function returns the sine of x. The argument is expressed in radians.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is either
+∞ 𝔽 or-∞ 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the sine ofℝ (n).
21.3.2.31 Math.sinh ( x )
This function returns the hyperbolic sine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - Return an
implementation-approximated Number value representing the result of the hyperbolic sine ofℝ (n).
The value of Math.sinh(x)
is the same as the value of (Math.exp(x) - Math.exp(-x)) / 2
.
21.3.2.32 Math.sqrt ( x )
This function returns the square root of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽,-0 𝔽, or+∞ 𝔽, return n. - If n <
-0 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the square root ofℝ (n).
21.3.2.33 Math.tan ( x )
This function returns the tangent of x. The argument is expressed in radians.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is either
+∞ 𝔽 or-∞ 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the result of the tangent ofℝ (n).
21.3.2.34 Math.tanh ( x )
This function returns the hyperbolic tangent of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is
+∞ 𝔽, return1 𝔽. - If n is
-∞ 𝔽, return-1 𝔽. - Return an
implementation-approximated Number value representing the result of the hyperbolic tangent ofℝ (n).
The value of Math.tanh(x)
is the same as the value of (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x))
.
21.3.2.35 Math.trunc ( x )
This function returns the integral part of the number x, removing any fractional digits. If x is already integral, the result is x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - If n <
1 𝔽 and n >+0 𝔽, return+0 𝔽. - If n <
-0 𝔽 and n >-1 𝔽, return-0 𝔽. - Return the
integral Number nearest n in the direction of+0 𝔽.