Standard Library: Math

Mathematical functions and constants

For more information on numbers see the language reference.

  1. abs
  2. acos
  3. asin
  4. atan
  5. atan2
  6. ceil
  7. clamp
  8. cos
  9. e
  10. floor
  11. inf
  12. isEven
  13. isInt
  14. isOdd
  15. ln
  16. log10
  17. log2
  18. logBase
  19. max
  20. min
  21. nan
  22. pi
  23. round
  24. roundTo
  25. sign
  26. sin
  27. sqrt
  28. tan
  29. tau
  30. toDegrees
  31. toRadians
  32. wrap

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

Math.min(a, b)

Return the minimum of a and b.

Math.min(-17, 7)
-17

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

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

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)