Working with Lua's Math Library

Lua's math library contains a collection of functions that are basically a subset of C's math library.

The math library is always available, and its functions can be accessed by name as shown in the following example:

print(math.abs(-123)) -- 123

The math library contains the following functions:

math.abs (x)
Returns the absolute value of x
math.acos (x)
Returns the arc-cosine of x, in radians.
math.asin (x)
Returns the arc-sine of x, in radians.
math.atan (x)
Returns the arc-tangent of x, in radians.
math.atan2 (x, y)
Returns the arc-tangent of y/x, in radians, based on the signs of both values to determine the correct quadrant.
math.ceil (x)
Returns the smallest integer value greater than or equal to x.
math.cos (x)
Returns the cosine of x, where x is specified in radians.
math.deg (x)
Convert x, specified in radians, to degrees
math.exp (x)
Returns the value of e raised to the xth power.
math.floor (x)
Returns the largest integer value less than or equal to x.
math.fmod (x, y)
Return a value equal to x - n*y for some integer n such that the result has the same sign as x and magnitude less than abs(y).
math.frexp (x)
Returns values (m, e) such that m * 2^e == x.
math.ldexp (x, y)
Return x * (2**y), essentially the inverse of function math.frexp.
math.log (x)
Return the natural log of x
math.log10 (x)
Return the base-10 logarithm of x
math.max (x, ...)
Returns the maximum value among its arguments.
math.min (x, ...)
Returns the minimum value among its arguments.
math.modf (x)
Spits real number x into it's integer and fractional parts.
math.pi
The constant pi, approximately equal to 3.14159.
math.pow (x, y)
Returns x raised to the power of y.
math.rad (x)
Convert x, specified in degrees, to radians
math.sin (x)
Returns the sine of x, where x is specified in radians.
math.sqrt (x)
Returns the square root of x
math.tan (x)
Returns the tangent of x, where x is specified in radians.
math.random ()
Return a uniformly-distributed pseudo-random number between 0 (inclusive) and 1
math.random (x)
Return a uniformly-distributed pseudo-random number between 1 (inclusive) and x
math.random (x, y)
Return a uniformly-distributed, pseudo-random number between x (inclusive) and y (inclusive).
math.randomseed (x)
Sets x as the "random seed" for the pseudo-random generator