Class Math

Mathematical functions and constants.

ConstantDescription
E: NumberThe constant e, which is the base of natural logarithms.
LN10: NumberThe natural logarithm of 10.
LN2: NumberThe natural logarithm of 2.
LOG10E: NumberThe base-10 logarithm of e.
LOG2E: NumberThe base-2 logarithm of e.
PI: NumberThe constant for PI.
SQRT1_2: Number1 divided by the square root of 2.
SQRT2: NumberThe square root of 2.
ConstructorDescription
Math()
MethodDescription
static abs(Number)Returns the absolute value of x.
static acos(Number)Returns an approximation to the arc cosine of x.
static acosh(Number)Returns an approximation to the inverse hyperbolic cosine of x.
static asin(Number)Returns an approximation to the arc sine of x.
static asinh(Number)Returns an approximation to the inverse hyperbolic sine of x.
static atan(Number)Returns an approximation to the arc tangent of x.
static atan2(Number, Number)Returns an approximation to the arc tangent of the quotient y/x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result.
static atanh(Number)Returns an approximation to the inverse hyperbolic tangent of x.
static cbrt(Number)Returns an approximation to the cube root of x.
static ceil(Number)Returns the smallest (closest to -∞) number value that is not less than x and is equal to a mathematical integer.
static clz32(Number)Returns the number of leading zero bits in the 32-bit binary representation of x.
static cos(Number)Returns an approximation to the cosine of x.
static cosh(Number)Returns an approximation to the hyperbolic cosine of x.
static exp(Number)Returns an approximation to the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).
static expm1(Number)Returns an approximation to subtracting 1 from the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).
static floor(Number)Returns the greatest (closest to +∞) number value that is not greater than x and is equal to a mathematical integer.
static fround(Number)Returns the nearest 32-bit single precision float representation of x.
static hypot(Number...)Returns an approximation of the square root of the sum of squares of the arguments.
static imul(Number, Number)Performs a 32 bit integer multiplication, where the result is always a 32 bit integer value, ignoring any overflows.
static log(Number)Returns an approximation to the natural logarithm of x.
static log10(Number)Returns an approximation to the base 10 logarithm of x.
static log1p(Number)Returns an approximation to the natural logarithm of of 1 + x.
static log2(Number)Returns an approximation to the base 2 logarithm of x.
static max(Number...)Returns the largest specified values.
static min(Number...)Returns the smallest of the specified values.
static pow(Number, Number)Returns an approximation to the result of raising x to the power y.
static random()Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy.
static round(Number)Returns the number value that is closest to x and is equal to a mathematical integer.
static sign(Number)Returns the sign of x, indicating whether x is positive, negative, or zero.
static sin(Number)Returns an approximation to the sine of x.
static sinh(Number)Returns an approximation to the hyperbolic sine of x.
static sqrt(Number)Returns an approximation to the square root of x.
static tan(Number)Returns an approximation to the tangent of x.
static tanh(Number)Returns an approximation to the hyperbolic tangent of x.
static trunc(Number)Returns the integral part of the number x, removing any fractional digits.

assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values

E: Number

The constant e, which is the base of natural logarithms.


LN10: Number

The natural logarithm of 10.


LN2: Number

The natural logarithm of 2.


LOG10E: Number

The base-10 logarithm of e.


LOG2E: Number

The base-2 logarithm of e.


PI: Number

The constant for PI.


SQRT1_2: Number

1 divided by the square root of 2.


SQRT2: Number

The square root of 2.


Math()


static abs(x: Number): Number

Returns the absolute value of x. The result has the same magnitude as x but has positive sign.

  • If _x_is NaN, the result is NaN.
  • If _x_is -0, the result is +0.
  • If _x_is -∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • the absolute value of x.

static acos(x: Number): Number

Returns an approximation to the arc cosine of x. The result is expressed in radians and ranges from +0 to +p.

  • If _x_is NaN, the result is NaN.
  • If _x_is greater than 1, the result is NaN.
  • If _x_is less than -1, the result is NaN.
  • If _x_is exactly 1, the result is +0.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the arc cosine of x.

static acosh(x: Number): Number

Returns an approximation to the inverse hyperbolic cosine of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is less than 1, the result is NaN.
  • If _x_is exactly 1, the result is +0.
  • If _x_is +∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the inverse hyperbolic cosine of x.

API Version:

Available from version 21.2.


static asin(x: Number): Number

Returns an approximation to the arc sine of x. The result is expressed in radians and ranges from -p/2 to +p/2.

  • If _x_is NaN, the result is NaN
  • If _x_is greater than 1, the result is NaN.
  • If _x_is less than -1, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the arc sine of x.

static asinh(x: Number): Number

Returns an approximation to the inverse hyperbolic sine of x.

  • If _x_is NaN, the result is NaN
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is -∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the inverse hyperbolic sine of x.

API Version:

Available from version 21.2.


static atan(x: Number): Number

Returns an approximation to the arc tangent of x. The result is expressed in radians and ranges from -p/2 to +p/2.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is an approximation to +p/2.
  • If _x_is -∞, the result is an approximation to -p/2.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the arc tangent of x.

static atan2(y: Number, x: Number): Number

Returns an approximation to the arc tangent of the quotient y/x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result. Note that it is intentional and traditional for the two-argument arc tangent function that the argument named y be first and the argument named x be second. The result is expressed in radians and ranges from -p to +p.

  • If either _x_or _y_is NaN, the result is NaN.
  • If y>0 and _x_is +0, the result is an implementation-dependent approximation to +p/2.
  • If y>0 and _x_is -0, the result is an implementation-dependent approximation to +p/2.
  • If _y_is +0 and x>0, the result is +0.
  • If _y_is +0 and _x_is +0, the result is +0.
  • If _y_is +0 and _x_is -0, the result is an implementation-dependent approximation to +p.
  • If _y_is +0 and X<0, the result is an implementation-dependent approximation to +p.
  • If _y_is -0 and x>0, the result is -0.
  • If _y_is -0 and _x_is +0, the result is -0.
  • If _y_is -0 and _x_is -0, the result is an implementation-dependent approximation to -p.
  • If _y_is -0 and X<0, the result is an implementation-dependent approximation to -p.
  • If y<0 and _x_is +0, the result is an implementation-dependent approximation to -p/2.
  • If y<0 and _x_is -0, the result is an implementation-dependent approximation to -p/2.
  • If y>0 and _y_is finite and _x_is +∞, the result is +0.
  • If y>0 and _y_is finite and _x_is -∞, the result if an implementation-dependent approximation to +p.
  • If y<0 and _y_is finite and _x_is +∞, the result is -0.
  • If y<0 and _y_is finite and _x_is -∞, the result is an implementation-dependent approximation to -p.
  • If _y_is +∞and _x_is finite, the result is an implementation-dependent approximation to +p/2.
  • If _y_is -∞and _x_is finite, the result is an implementation-dependent approximation to -p/2.
  • If _y_is +∞and _x_is +∞, the result is an implementation-dependent approximation to +p/4.
  • If _y_is +∞and _x_is -∞, the result is an implementation-dependent approximation to +3p/4.
  • If _y_is -∞and _x_is +∞, the result is an implementation-dependent approximation to -p/4.
  • If _y_is -∞and _x_is -∞, the result is an implementation-dependent approximation to -3p/4.

Parameters:

  • y - the first argument.
  • x - the second argument.

Returns:

  • approximation to the arc tangent of the quotient y/x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result.

static atanh(x: Number): Number

Returns an approximation to the inverse hyperbolic tangent of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is less than -1, the result is NaN.
  • If _x_is greater than 1, the result is NaN.
  • If _x_is exactly -1, the result is -∞.
  • If _x_is exactly +1, the result is +∞.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the inverse hyperbolic tangent of x.

API Version:

Available from version 21.2.


static cbrt(x: Number): Number

Returns an approximation to the cube root of x.

  • If _x_is NaN, the result is NaN
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is -∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the cube root of x.

API Version:

Available from version 21.2.


static ceil(x: Number): Number

Returns the smallest (closest to -∞) number value that is not less than x and is equal to a mathematical integer. If x is already an integer, the result is x.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is -∞.
  • If _x_is less than 0 but greater than -1, the result is -0.

The value of Math.ceil(x) is the same as the value of -Math.floor(-x).

Parameters:

  • x - the Number to operate on.

Returns:

  • the smallest (closest to -∞) number value that is not less than x and is equal to a mathematical integer.

static clz32(x: Number): Number

Returns the number of leading zero bits in the 32-bit binary representation of x.

Parameters:

  • x - the Number to operate on.

Returns:

  • the number of leading zero bits in the 32-bit binary representation of x.

API Version:

Available from version 21.2.


static cos(x: Number): Number

Returns an approximation to the cosine of x. The argument is expressed in radians.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is 1.
  • If _x_is -0, the result is 1.
  • If _x_is +∞, the result is NaN.
  • If _x_is -∞, the result is NaN.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the cosine of x.

static cosh(x: Number): Number

Returns an approximation to the hyperbolic cosine of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is 1.
  • If _x_is -0, the result is 1.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the hyperbolic cosine of x.

API Version:

Available from version 21.2.


static exp(x: Number): Number

Returns an approximation to the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is 1.
  • If _x_is -0, the result is 1.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is +0.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the exponential function of x.

static expm1(x: Number): Number

Returns an approximation to 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 0.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is -1.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to subtracting 1 from the exponential function of x.

API Version:

Available from version 21.2.


static floor(x: Number): Number

Returns the greatest (closest to +∞) number value that is not greater than x and is equal to a mathematical integer. If x is already an integer, the result is x.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is -∞.
  • If _x_is greater than 0 but less than 1, the result is +0.

The value of Math.floor(x) is the same as the value of -Math.ceil(-x).

Parameters:

  • x - the Number to operate on.

Returns:

  • the greatest (closest to +∞) number value that is not greater than x and is equal to a mathematical integer.

static fround(x: Number): Number

Returns the nearest 32-bit single precision float representation of x.

Parameters:

  • x - the Number to operate on.

Returns:

  • the nearest 32-bit single precision float representation of x.

API Version:

Available from version 21.2.


static hypot(values: Number...): Number

Returns an approximation of the square root of the sum of squares of the arguments.

  • If no arguments are passed, the result is +0.
  • If any argument is +∞, the result is +∞.
  • If any argument is -∞, the result is +∞.
  • If no argument is +∞or -∞and any argument is NaN, the result is NaN.
  • If all arguments are either +0 or -0, the result is +0.

Parameters:

  • values - the Number values to operate on.

Returns:

  • an approximation of the square root of the sum of squares of the arguments.

API Version:

Available from version 21.2.


static imul(x: Number, y: Number): Number

Performs a 32 bit integer multiplication, where the result is always a 32 bit integer value, ignoring any overflows.

Parameters:

  • x - The first operand.
  • y - The second operand.

Returns:

  • Returns the result of the 32 bit multiplication. The result is a 32 bit signed integer value.

API Version:

Available from version 21.2.


static log(x: Number): Number

Returns an approximation to the natural logarithm of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is less than 0, the result is NaN.
  • If _x_is +0 or -0, the result is -∞.
  • If _x_is 1, the result is +0.
  • If _x_is +∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the natural logarithm of x.

static log10(x: Number): Number

Returns an approximation to the base 10 logarithm of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is less than 0, the result is NaN.
  • If _x_is +0 or -0, the result is -∞.
  • If _x_is 1, the result is +0.
  • If _x_is +∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the base 10 logarithm of x.

API Version:

Available from version 21.2.


static log1p(x: Number): Number

Returns an approximation to the natural logarithm of of 1 + x.

  • If _x_is NaN, the result is NaN.
  • If _x_is less than -1, the result is NaN.
  • If _x_is -1, the result is -∞.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the natural logarithm of of 1 + x.

API Version:

Available from version 21.2.


static log2(x: Number): Number

Returns an approximation to the base 2 logarithm of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is less than 0, the result is NaN.
  • If _x_is +0 or -0, the result is -∞.
  • If _x_is 1, the result is +0.
  • If _x_is +∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the base 2 logarithm of x.

API Version:

Available from version 21.2.


static max(values: Number...): Number

Returns the largest specified values. If no arguments are given, the result is -∞. If any value is NaN, the result is NaN.

Parameters:

  • values - zero or more values.

Returns:

  • the largest of the specified values.

static min(values: Number...): Number

Returns the smallest of the specified values. If no arguments are given, the result is +∞. If any value is NaN, the result is NaN.

Parameters:

  • values - zero or more values.

Returns:

  • the smallest of the specified values.

static pow(x: Number, y: Number): Number

Returns an approximation to the result of raising x to the power y.

  • If _y_is NaN, the result is NaN.
  • If _y_is +0, the result is 1, even if _x_is NaN.
  • If _y_is -0, the result is 1, even if _x_is NaN.
  • If _x_is NaN and _y_is nonzero, the result is NaN.
  • If abs(x)>1 and _y_is +∞, the result is +∞.
  • If abs(x)>1 and _y_is -∞, the result is +0.
  • If abs(x)==1 and _y_is +∞, the result is NaN.
  • If abs(x)==1 and _y_is -∞, the result is NaN.
  • If abs(x)<1 and _y_is +∞, the result is +0.
  • If abs(x)<1 and _y_is -∞, the result is +∞.
  • If _x_is +∞and y>0, the result is +∞.
  • If _x_is +∞and y<0, the result is +0.
  • If _x_is -∞and y>0 and _y_is an odd integer, the result is -∞.
  • If _x_is -∞and y>0 and _y_is not an odd integer, the result is +∞.
  • If _x_is -∞and y<0 and _y_is an odd integer, the result is -0.
  • If _x_is -∞and y<0 and _y_is not an odd integer, the result is +0.
  • If _x_is +0 and y>0, the result is +0.
  • If _x_is +0 and y<0, the result is +∞.
  • If _x_is -0 and y>0 and _y_is an odd integer, the result is -0.
  • If _x_is -0 and y>0 and _y_is not an odd integer, the result is +0.
  • If _x_is -0 and y<0 and _y_is an odd integer, the result is -∞.
  • If _x_is -0 and y<0 and _y_is not an odd integer, the result is +∞.
  • If X<0 and _x_is finite and _y_is finite and _y_is not an integer, the result is NaN.

Parameters:

  • x - a Number that will be raised to the power of y.
  • y - the power by which x will be raised.

Returns:

  • an approximation to the result of raising x to the power y.

static random(): Number

Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy.

Returns:

  • a Number greater than or equal to 0 but less than 1.

static round(x: Number): Number

Returns the number value that is closest to x and is equal to a mathematical integer. If two integer number values are equally close to x, then the result is the number value that is closer to +∞. If x is already an integer, the result is x.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is -∞.
  • If _x_is greater than 0 but less than 0.5, the result is +0.
  • If _x_is less than 0 but greater than or equal to -0.5, the result is -0.

Math.round(3.5) returns 4, but Math.round(-3.5) returns -3. The value of Math.round(x) is the same as the value of Math.floor(x+0.5), except when x is -0 or is less than 0 but greater than or equal to -0.5; for these cases Math.round(x) returns -0, but Math.floor(x+0.5) returns +0.

Parameters:

  • x - the Number to operate on.

Returns:

  • the number value that is closest to x and is equal to a mathematical integer.

static sign(x: Number): Number

Returns the sign of x, indicating whether x is positive, negative, or zero.

  • If _x_is NaN, the result is NaN.
  • If _x_is -0, the result is -0.
  • If _x_is +0, the result is +0.
  • If _x_is negative and not -0, the result is -1.
  • If _x_is positive and not +0, the result is +1.

Parameters:

  • x - the Number to operate on.

Returns:

  • the sign of x.

API Version:

Available from version 21.2.


static sin(x: Number): Number

Returns an approximation to the sine of x. The argument is expressed in radians.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞or -∞, the result is NaN.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the sine of x.

static sinh(x: Number): Number

Returns an approximation to the hyperbolic sine of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.
  • If _x_is -∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the hyperbolic sine of x.

API Version:

Available from version 21.2.


static sqrt(x: Number): Number

Returns an approximation to the square root of x.

  • If _x_is NaN, the result is NaN.
  • If _x_isless than 0, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +∞.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the square root of x.

static tan(x: Number): Number

Returns an approximation to the tangent of x. The argument is expressed in radians.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞or -∞, the result is NaN.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the tangent of x.

static tanh(x: Number): Number

Returns an approximation to the hyperbolic tangent of x.

  • If _x_is NaN, the result is NaN.
  • If _x_is +0, the result is +0.
  • If _x_is -0, the result is -0.
  • If _x_is +∞, the result is +1.
  • If _x_is -∞, the result is -1.

Parameters:

  • x - the Number to operate on.

Returns:

  • an approximation to the hyperbolic tangent of x.

API Version:

Available from version 21.2.


static trunc(x: Number): Number

Returns the integral part of the number x, removing any fractional digits. If x is already an integer, the result is x.

  • If _x_is NaN, the result is NaN.
  • If _x_is -0, the result is -0.
  • If _x_is +0, the result is +0.
  • If _x_is -∞, the result is -∞.
  • If _x_is +∞, the result is +∞.
  • If _x_is greater than 0 but less than 1, the result is +0.
  • If _x_is less than 0 but greater than -1, the result is -0.

Parameters:

  • x - the Number to operate on.

Returns:

  • the integral part of the number of x.

API Version:

Available from version 21.2.