The Math
object is a built-in object that has some mathematical constants and mathematical function methods. The Math
object is used with the Number
type and does not support BigInt
.
Math
is not a function object, meaning it's not a constructor. All properties and methods of Math
are static. For example, the way to reference the constant representing the mathematical constant pi is Math.PI
. The constants of Math
are defined using full-precision floating point numbers in JavaScript. It should be noted that many of the functions in Math
have a precision, and this precision is also different in different implementations. This means that different browsers will produce different results, and even the same Js
engine may produce different results on different systems or architectures. Furthermore, the values returned by trigonometric functions sin()
, cos()
, tan()
, asin()
, acos()
, atan()
, and atan2()
are in radians, not degrees. To convert, radians can be divided by Math.PI / 180
to convert to degrees, and multiplying degrees by this number will convert them to radians.
Math.E
: Euler's constant, also known as the base of natural logarithms, approximately equal to 2.718
.Math.LN2
: Natural logarithm of 2
, approximately equal to 0.693
.Math.LN10
: Natural logarithm of 10
, approximately equal to 2.303
.Math.LOG2E
: Logarithm of E
to base 2
, approximately equal to 1.443
.Math.LOG10E
: Logarithm of E
to base 10
, approximately equal to 0.434
.Math.PI
: The mathematical constant pi, approximately equal to 3.14159
, represents the ratio of the circumference of a circle to its diameter.Math.SQRT1_2
: Square root of ½
, also the reciprocal of the square root of 2
, approximately equal to 0.707
.Math.SQRT2
: Square root of 2
, approximately equal to 1.414
.Math.abs(x)
The Math.abs(x)
function returns the absolute value of the specified number x
.
Math.acos(x)
Math.acos()
returns the arccosine of a number.
∀x∊[-1;1], Math.acos(x) = arccos(x) = the unique y∊[0;π] such that cos(y) = x
Math.acosh(x)
Math.acosh()
returns the hyperbolic arccosine of a number.
∀x≥1, Math.acosh(x) = arcosh(x) = the unique y≥0 such that cosh(y) = x
Math.asin(x)
Math.asin()
returns the arcsine of a number.
∀x∊[-1;1], Math.asin(x) = arcsin(x) = the unique y∊[- π/2 ; π/2 ] such that sin(y) = x
Math.asinh(x)
Math.asinh()
returns the hyperbolic arcsine of a number.
Math.asinh(x) = arsinh(x) = the unique y such that sinh(y) = x
Math.atan(x)
Math.atan()
returns the arctangent of a number.
Math.atan(x) = arctan(x) = the unique y∊[- π/2 ; π/2 ] such that tan(y) = x
Math.atanh(x)
Math.atanh()
returns the hyperbolic arctangent of a number.
∀x∊(-1,1), Math.atanh(x) = arctanh(x) = the unique y such that tanh(y) = x
Math.atan2(y, x)
Math.atan2()
returns the angle (in radians) from the x
-axis to a point (x, y)
in the plane. It's the equivalent of Math.atan2(y,x)
.
Math.cbrt(x)
Math.cbrt()
returns the cube root of a number.
Math.ceil(x)
Math.ceil()
returns the smallest integer greater than or equal to a given number, in other words, it rounds up.
Math.clz32(x)
Math.clz32()
returns the number of leading zero bits in the 32-bit binary representation of a number. For example, 1000000
in its 32-bit unsigned integer binary form is 00000000000011110100001001000000
, which has 12
leading zero bits, therefore Math.clz32(1000000)
returns 12
.
Math.cos(x)
Math.cos()
returns the cosine of a number.
Math.cosh(x)
The Math.cosh()
function returns the hyperbolic cosine of a number.
Math.exp(x)
The Math.exp()
function returns e^x
, where x
is the parameter and e
is the base of the natural logarithms, approximately 2.718281828459045
.
Math.expm1(x)
The Math.expm1()
function returns e^x -1
, where x
is the parameter and e
is the base of the natural logarithms, approximately 2.718281828459045
.
Math.floor()
returns the largest integer less than or equal to a given number, i.e., it performs the floor operation.
Math.fround(doubleFloat)
The Math.fround()
function can convert any number to the nearest single precision floating point number. JavaScript internally uses 64
-bit double-precision numbers, which support high precision. However, there are situations when a 32
-bit floating point number is needed, such as when reading values from a Float32Array
, which can lead to confusion. When comparing a 64
-bit floating point number and a 32
-bit floating point number, even if the two numbers are almost identical, the comparison fails. To solve this problem, Math.fround()
can be used to convert a 64
-bit floating point number to a 32
-bit floating point number, internally in JavaScript, the number is still treated as a 64
-bit floating point number, but only the 23rd bit of the significand is rounded to even and the subsequent bits are set to 0
. If the number exceeds the range of a 32
-bit floating point number, it will return Infinity
or -Infinity
.
Math.hypot([value1[,value2, ...]])
The Math.hypot()
function returns the square root of the sum of the squares of its arguments. This function is simpler and faster than Math.sqrt()
as it only requires calling Math.hypot(v1, v2)
or Math.hypot(v1, v2, v3, v4, ...)
. It also avoids problems with excessively large magnitudes. In JavaScript, the largest double-precision floating point number is Number.MAX_VALUE = 1.797...e+308
. If a calculated number is greater than approximately 1e154
, squaring it will result in Infinity
and cause issues with the calculation's results.
Math.imul(a, b)
The Math.imul()
function converts two parameters to 32-bit integers, multiplies them together, and returns a 32-bit result, similar to the way 32-bit integers are multiplied in the C language.
Math.log(x)
The Math.log()
function returns the natural logarithm of a number.
∀x>0, Math.log(x) = ln(x) = the unique y such that e^y = x
Math.log10(x)
The Math.log10()
function returns the base 10
logarithm of a number.
Math.log1p(x)
The Math.log1p()
function returns the natural logarithm of a number plus 1
, in other words, log(x+1)
.
Math.log2(x)
The Math.log2()
function returns the base 2
logarithm of a number.
Math.max(value1[,value2, ...])
The Math.max()
function returns the largest of a set of numbers.
Math.min([value1[,value2, ...]])
Math.min()
returns the smallest of zero or more numbers.
Math.pow(base, exponent)
The Math.pow()
function returns the base raised to the exponent power, that is, base^exponent
.
Math.random()
The Math.random()
function returns a floating-point, pseudo-random number between 0
(inclusive) and 1
(exclusive), meaning it can return any value from 0 up to but not including 1. It can be scaled to the desired range, allowing the initial seed for the random number generation algorithm to be chosen and the algorithm cannot be reset or chosen by the user.
Math.round(x)
The Math.round()
function returns the value of a number rounded to the nearest integer.
Math.sign(x)
The Math.sign()
function returns the sign of a number, indicating whether the number is positive, negative, or zero. This function has five possible return values: 1, -1, 0, -0, NaN
, representing positive, negative, positive zero, negative zero, and NaN
respectively.
Math.sin(x)
The Math.sin()
function returns the sine of a number.
Math.sinh(x)
The Math.sinh()
function returns the hyperbolic sine of a number.
Math.sqrt(x)
The Math.sqrt()
function returns the square root of a number.
∀x≥0, Math.sqrt(x) = x = the unique y≥0 such that y^2 = x
Math.tan(x)
The Math.tan()
method returns the tangent of a number.
Math.tanh(x)
The Math.tanh()
function will return the hyperbolic tangent of a number.
Math.trunc(value)
The Math.trunc()
method will remove the decimal part of a number, leaving only the integer part.