Math
Math
is an object with data properties and methods for processing numbers. You can see it as a poor man’s module: It was created long before JavaScript had modules.
Math.E: number
ES1
Euler’s number, base of the natural logarithms, approximately 2.7182818284590452354.
Math.LN10: number
ES1
The natural logarithm of 10, approximately 2.302585092994046.
Math.LN2: number
ES1
The natural logarithm of 2, approximately 0.6931471805599453.
Math.LOG10E: number
ES1
The logarithm of e to base 10, approximately 0.4342944819032518.
Math.LOG2E: number
ES1
The logarithm of e to base 2, approximately 1.4426950408889634.
Math.PI: number
ES1
The mathematical constant π, ratio of a circle’s circumference to its diameter, approximately 3.1415926535897932.
Math.SQRT1_2: number
ES1
The square root of 1/2, approximately 0.7071067811865476.
Math.SQRT2: number
ES1
The square root of 2, approximately 1.4142135623730951.
Math.cbrt(x: number): number
ES6
Returns the cube root of x
.
> Math.cbrt(8)
2
Math.exp(x: number): number
ES1
Returns ex
(e being Euler’s number). The inverse of Math.log()
.
> Math.exp(0)
1
> Math.exp(1) === Math.E
true
Math.expm1(x: number): number
ES6
Returns Math.exp(x)-1
. The inverse of Math.log1p()
. Very small numbers (fractions close to 0) are represented with a higher precision. Therefore, this function returns more precise values whenever .exp()
returns values close to 1.
Math.log(x: number): number
ES1
Returns the natural logarithm of x
(to base e, Euler’s number). The inverse of Math.exp()
.
> Math.log(1)
0
> Math.log(Math.E)
1
> Math.log(Math.E ** 2)
2
Math.log1p(x: number): number
ES6
Returns Math.log(1 + x)
. The inverse of Math.expm1()
. Very small numbers (fractions close to 0) are represented with a higher precision. Therefore, you can provide this function with a more precise argument whenever the argument for .log()
is close to 1.
Math.log10(x: number): number
ES6
Returns the logarithm of x
to base 10. The inverse of 10 ** x
.
> Math.log10(1)
0
> Math.log10(10)
1
> Math.log10(100)
2
Math.log2(x: number): number
ES6
Returns the logarithm of x
to base 2. The inverse of 2 ** x
.
> Math.log2(1)
0
> Math.log2(2)
1
> Math.log2(4)
2
Math.pow(x: number, y: number): number
ES1
Returns x
y
, x
to the power of y
. The same as x ** y
.
> Math.pow(2, 3)
8
> Math.pow(25, 0.5)
5
Math.sqrt(x: number): number
ES1
Returns the square root of x
. The inverse of x ** 2
.
> Math.sqrt(9)
3
Rounding to integer means converting an arbitrary number to an integer (a number without a decimal fraction). The following functions implement several approaches for doing so.
Math.ceil(x: number): number
ES1
Returns the smallest (closest to −∞) integer i
with x
≤ i
.
> Math.ceil(2.1)
3
> Math.ceil(2.9)
3
Math.floor(x: number): number
ES1
Returns the largest (closest to +∞) integer i
with i
≤ x
.
> Math.floor(2.1)
2
> Math.floor(2.9)
2
Math.round(x: number): number
ES1
Returns the integer that is closest to x
. If the decimal fraction of x
is .5
then .round()
rounds up (to the integer closer to positive infinity):
> Math.round(2.4)
2
> Math.round(2.5)
3
Math.trunc(x: number): number
ES6
Removes the decimal fraction of x
and returns the resulting integer.
> Math.trunc(2.1)
2
> Math.trunc(2.9)
2
Table 19.1 shows the results of the rounding functions for a few representative inputs.
-2.9 | -2.5 | -2.1 | 2.1 | 2.5 | 2.9 |
|
---|---|---|---|---|---|---|
Math.floor | -3 | -3 | -3 | 2 | 2 | 2 |
Math.ceil | -2 | -2 | -2 | 3 | 3 | 3 |
Math.round | -3 | -2 | -2 | 2 | 3 | 3 |
Math.trunc | -2 | -2 | -2 | 2 | 2 | 2 |
Table 19.1: Rounding functions of Math
. Note how things change with negative numbers because “larger” always means “closer to positive infinity”.
Math.fround(x: number): number
ES6
Rounds x
to a 32-bit single float (within a 64-bit double float):
> Math.fround(2**128)
Infinity
> 2**128
3.402823669209385e+38
> Math.fround(2**-150)
0
> 2**-150
7.006492321624085e-46
Math.f16round(x: number): number
ES2025
Rounds x
to a 16-bit half-float (within a 64-bit double float):
> Math.f16round(2**16)
Infinity
> 2**16
65536
> Math.f16round(2**-25)
0
> 2**-25
2.9802322387695312e-8
Math.f16round(x)
rounds x
to a 16-bit half-float (within a 64-bit double float).
If there is positive overflow (positive numbers being too far away from zero), the result is positive infinity:
> Math.f16round(2**15)
32768
> Math.f16round(2**16)
Infinity
> 2**16
65536
If there is negative overflow (negative numbers being too far away from zero), the result is negative infinity:
> Math.f16round(-(2**15))
-32768
> Math.f16round(-(2**16))
-Infinity
> -(2**16)
-65536
Arithmetic underflow means that a number has too many digits after a binary point (is too close to an integer). If that happens, digits that can’t be represented are omitted:
> Math.f16round(2**-24)
5.960464477539063e-8
> Math.f16round(2**-25)
0
> 2**-25
2.9802322387695312e-8
All angles are specified in radians. Use the following two functions to convert between degrees and radians.
function degreesToRadians(degrees) {
return degrees / 180 * Math.PI;
}
assert.equal(degreesToRadians(90), Math.PI/2);
function radiansToDegrees(radians) {
return radians / Math.PI * 180;
}
assert.equal(radiansToDegrees(Math.PI), 180);
Math.acos(x: number): number
ES1
Returns the arc cosine (inverse cosine) of x
.
> Math.acos(0)
1.5707963267948966
> Math.acos(1)
0
Math.acosh(x: number): number
ES6
Returns the inverse hyperbolic cosine of x
.
Math.asin(x: number): number
ES1
Returns the arc sine (inverse sine) of x
.
> Math.asin(0)
0
> Math.asin(1)
1.5707963267948966
Math.asinh(x: number): number
ES6
Returns the inverse hyperbolic sine of x
.
Math.atan(x: number): number
ES1
Returns the arc tangent (inverse tangent) of x
.
Math.atanh(x: number): number
ES6
Returns the inverse hyperbolic tangent of x
.
Math.atan2(y: number, x: number): number
ES1
Returns the arc tangent of the quotient y/x.
Math.cos(x: number): number
ES1
Returns the cosine of x
.
> Math.cos(0)
1
> Math.cos(Math.PI)
-1
Math.cosh(x: number): number
ES6
Returns the hyperbolic cosine of x
.
Math.hypot(...values: Array<number>): number
ES6
Returns the square root of the sum of the squares of values
(Pythagoras’ theorem):
> Math.hypot(3, 4)
5
Math.sin(x: number): number
ES1
Returns the sine of x
.
> Math.sin(0)
0
> Math.sin(Math.PI / 2)
1
Math.sinh(x: number): number
ES6
Returns the hyperbolic sine of x
.
Math.tan(x: number): number
ES1
Returns the tangent of x
.
> Math.tan(0)
0
> Math.tan(1)
1.5574077246549023
Math.tanh(x: number): number;
ES6
Returns the hyperbolic tangent of x
.
Math.abs(x: number): number
ES1
Returns the absolute value of x
.
> Math.abs(3)
3
> Math.abs(-3)
3
> Math.abs(0)
0
Math.clz32(x: number): number
ES6
Counts the leading zero bits in the 32-bit integer x
. Used in DSP algorithms.
> Math.clz32(0b01000000000000000000000000000000)
1
> Math.clz32(0b00100000000000000000000000000000)
2
> Math.clz32(2)
30
> Math.clz32(1)
31
Math.max(...values: Array<number>): number
ES1
Converts values
to numbers and returns the largest one.
> Math.max(3, -5, 24)
24
Math.min(...values: Array<number>): number
ES1
Converts values
to numbers and returns the smallest one.
> Math.min(3, -5, 24)
-5
Math.random(): number
ES1
Returns a pseudo-random number n
where 0 ≤ n
< 1.
/** Returns a random integer i with 0 <= i < max */
function getRandomInteger(max) {
return Math.floor(Math.random() * max);
}
Math.sign(x: number): number
ES6
Returns the sign of a number:
> Math.sign(-8)
-1
> Math.sign(0)
0
> Math.sign(3)
1