Standard Library: Math

Mathematical functions and constants

For more information on numbers see the language reference.


Module Contents


Math.abs(n)

Return the absolute value of n.

Math.abs(-10)
10

Math.acos(n)

Return the arccosine (inverse cosine) of n, in radians.

Math.acos(0.5) -- Math.pi / 3
1.0471975511965979

Math.asin(n)

Return the arcsine (inverse sine) of n, in radians.

Math.asin(0.5) -- Math.pi / 6
0.5235987755982989

Math.atan(n)

Return the arctangent (inverse tangent) of n, in radians.

Math.atan(1) -- Math.pi / 4
0.7853981633974483

Math.atan2(y, x)

Return the arctangent (inverse tangent) of y / x, using the signs to determine the correct quadrant.

Math.atan2(1, -1) -- 3 * Math.pi / 4
2.356194490192345

Math.ceil(n)

Round n up to the nearest integer.

Math.ceil(1.5)
2

Math.clamp(n, min, max)

Clamp n within the range min and max.

Math.clamp(17, 10, 20)
Math.clamp(21, 10, 20)
Math.clamp(9, 10, 20)
17
20
10

Math.cos(n)

Return the cosine of n radians.

Math.cos(Math.pi / 3)
0.5000000000000001

Math.e

Euler's number.

2.718281828459045

Math.floor(n)

Round n down to the nearest integer.

Math.floor(1.5)
1

Math.inf

Floating-point infinity.

Math.inf

Math.isEven(n)

Check whether an integer n is even.

Math.isEven(-6)
Math.isEven(-7)
true
false

Math.isInt(n)

Check whether n is an integer (whole number).

Math.isInt(42)
Math.isInt(3.14)
true
false

Math.isOdd(n)

Check whether an integer n is odd.

Math.isOdd(-6)
Math.isOdd(-7)
false
true

Math.ln(n)

Return the natural logarithm (base e) of n.

Math.ln(Math.e ** 5)
5

Math.log10(n)

Return the base-10 logarithm of n.

Math.log10(100)
2

Math.log2(n)

Return the base-2 logarithm of n.

Math.log2(256)
8

Math.logBase(a, b)

Return the logarithm of a with base b.

Math.logBase(625, 5)
4

Math.max(a, b)

Return the maximum of a and b.

Math.max(-17, 7)
7

(Accessible as a global through Overloads.max)


Math.min(a, b)

Return the minimum of a and b.

Math.min(-17, 7)
-17

(Accessible as a global through Overloads.min)


Math.nan

"Not a number". Cursed floating-point nonsense.

Math.nan

Math.pi

The constant π (pi).

3.141592653589793

Math.round(n)

Round n to the nearest integer using banker's rounding.

Math.round(3.14)
Math.round(2.5)
Math.round(3.5)
3
2
4

(Accessible as a global through Overloads.round)


Math.roundTo(n, decimals)

Round n to decimals places using banker's rounding. If decimals is negative then rounding is done to the corresponding positive power of 10.

Math.roundTo(17.45, 1)
Math.roundTo(17.45, -1)
17.4
20

(Accessible as a global through Overloads.roundTo)


Math.sign(n)

Return the sign of n.

Math.sign(7)
Math.sign(-7)
Math.sign(0)
1
-1
0

Math.sin(n)

Return the cosine of n radians.

Math.sin(Math.pi / 6)
0.49999999999999994

Math.sqrt(n)

Return the square root of a positive number n.

Math.sqrt(2)
1.4142135623730951

Math.tan(n)

Return the tangent of n (in radians).

Math.tan(Math.pi / 3)
1.7320508075688767

Math.tau

2 * Math.pi. See The Tau Manifesto.

6.283185307179586

Math.toDegrees(radians)

Convert radians to degrees.

Math.toDegrees(Math.pi)
180

Math.toRadians(degrees)

Convert degrees to radians.

Math.toRadians(180)
3.141592653589793

Math.wrap(n, start, limit)