math

Mathematical functions of Lua 5.1 / LuaJIT 2.1. Angles are in radians unless stated otherwise.


Functions

math.abs(x: number): number

math.abs(x: number): number

  • Returns the absolute value of x.

math.acos(x: number): number

math.acos(x: number): number

  • Returns the arc cosine of x, in radians. x must be in [-1, 1], otherwise the result is NaN.

math.asin(x: number): number

math.asin(x: number): number

  • Returns the arc sine of x, in radians. x must be in [-1, 1], otherwise the result is NaN.

math.atan(x: number): number

math.atan(x: number): number

  • Returns the arc tangent of x, in radians (range -pi/2 to pi/2).

math.atan2(y: number, x: number): number

math.atan2(y: number, x: number): number

  • Returns the arc tangent of y / x in radians, using the signs of both to find the quadrant (range -pi to pi).

math.ceil(x: number): number

math.ceil(x: number): number

  • Returns the smallest integer value greater than or equal to x.

math.cos(x: number): number

math.cos(x: number): number

  • Returns the cosine of x.

math.cosh(x: number): number

math.cosh(x: number): number

  • Returns the hyperbolic cosine of x.

math.deg(x: number): number

math.deg(x: number): number

  • Converts x from radians to degrees.

math.exp(x: number): number

math.exp(x: number): number

  • Returns e raised to the power x.

math.floor(x: number): number

math.floor(x: number): number

  • Returns the largest integer value less than or equal to x.

math.fmod(x: number, y: number): number

math.fmod(x: number, y: number): number

  • Returns the remainder of x / y rounded toward zero, so the result has the sign of x. This differs from the % operator for negative operands.
print(math.fmod(-7, 3), -7 % 3)

math.frexp(x: number): number, number

math.frexp(x: number): number, number

  • Returns m and e such that x = m * 2^e, with e an integer and m in [0.5, 1) (or 0).

math.ldexp(m: number, e: number): number

math.ldexp(m: number, e: number): number

  • Returns m * 2^e. e should be an integer.

math.log(x: number, base?: number): number

math.log(x: number, base?: number): number

  • Returns the natural logarithm of x.
  • LuaJIT also accepts a second argument and returns the logarithm of x in that base.

math.log10(x: number): number

math.log10(x: number): number

  • Returns the base-10 logarithm of x.

math.max(x: number, ...: number): number

math.max(x: number, ...: number): number

  • Returns the largest of its arguments.

math.min(x: number, ...: number): number

math.min(x: number, ...: number): number

  • Returns the smallest of its arguments.

math.modf(x: number): number, number

math.modf(x: number): number, number

  • Returns the integral part of x (rounded toward zero) and the fractional part.
print(math.modf(-3.25))

math.pow(x: number, y: number): number

math.pow(x: number, y: number): number

  • Returns x raised to the power y. Same as x ^ y.

math.rad(x: number): number

math.rad(x: number): number

  • Converts x from degrees to radians.

math.random(m?: number, n?: number): number

math.random(m?: number, n?: number): number

  • With no argument, returns a float in [0, 1).
  • math.random(n) returns an integer in [1, n]; n must be at least 1.
  • math.random(m, n) returns an integer in [m, n]; m must not exceed n.
print(math.random(1, 6))

math.randomseed(seed: number)

math.randomseed(seed: number)

  • Sets seed as the seed of the pseudo-random generator. The same seed gives the same sequence.
math.randomseed(os.time())

math.sin(x: number): number

math.sin(x: number): number

  • Returns the sine of x.

math.sinh(x: number): number

math.sinh(x: number): number

  • Returns the hyperbolic sine of x.

math.sqrt(x: number): number

math.sqrt(x: number): number

  • Returns the square root of x. Negative x gives NaN.

math.tan(x: number): number

math.tan(x: number): number

  • Returns the tangent of x.

math.tanh(x: number): number

math.tanh(x: number): number

  • Returns the hyperbolic tangent of x.

Constants

math.huge: number

math.huge: number

  • Positive infinity. -math.huge is negative infinity.

math.pi: number

math.pi: number

  • The value of pi, 3.14159265358979...