Skip to main content

Lua Math Library

math

Standard Lua 5.1 math library.


math.pi

field

math.pi

The value of pi.

Usage

math.pi

Returns

  • number

Example

print(math.pi) -- 3.1415926535898

math.huge

field

math.huge

A value larger than any other numeric value.

A value larger than or equal to any other numeric value (floating-point infinity).

Usage

math.huge

Returns

  • number

Example

print(math.huge) -- inf

math.abs

function

math.abs(x)

Absolute value.

Returns the absolute value of x.

Usage

math.abs(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.abs(-5)) -- 5

math.ceil

function

math.ceil(x)

Round up to an integer.

Returns the smallest integer larger than or equal to x.

Usage

math.ceil(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • integer

Example

print(math.ceil(4.1)) -- 5

math.floor

function

math.floor(x)

Round down to an integer.

Returns the largest integer smaller than or equal to x.

Usage

math.floor(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • integer

Example

print(math.floor(4.9)) -- 4

math.sqrt

function

math.sqrt(x)

Square root.

Returns the square root of x.

Usage

math.sqrt(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.sqrt(16)) -- 4

math.pow

function

math.pow(x, y)

Exponentiation.

Returns x raised to the power y. Equivalent to the x^y operator.

Usage

math.pow(x, y)

Parameters

NameTypeRequiredDescription
xnumberYesBase.
ynumberYesExponent.

Returns

  • number

Example

print(math.pow(2, 10)) -- 1024

math.exp

function

math.exp(x)

e raised to a power.

Returns the value e^x.

Usage

math.exp(x)

Parameters

NameTypeRequiredDescription
xnumberYesExponent.

Returns

  • number

Example

print(math.exp(1)) -- 2.718281828459

math.log

function

math.log(x)

Natural logarithm.

Returns the natural logarithm of x.

Usage

math.log(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.log(math.exp(1))) -- 1

math.log10

function

math.log10(x)

Base-10 logarithm.

Returns the base-10 logarithm of x.

Usage

math.log10(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.log10(100)) -- 2

math.fmod

function

math.fmod(x, y)

Remainder of x/y.

Returns the remainder of the division of x by y that rounds the quotient towards zero.

Usage

math.fmod(x, y)

Parameters

NameTypeRequiredDescription
xnumberYesDividend.
ynumberYesDivisor.

Returns

  • number

Example

print(math.fmod(7, 3)) -- 1
print(math.fmod(-7, 3)) -- -1

math.modf

function

math.modf(x)

Split a number into integral and fractional parts.

Returns the integral part of x and the fractional part of x; both have the same sign as x.

Usage

local ip, fp = math.modf(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • integralPart, fractionalPart

Example

local Ip, Fp = math.modf(3.75)
print(Ip, Fp) -- 3 0.75

math.max

function

math.max(x, ...)

Largest of the given numbers.

Returns the argument with the maximum value, among a list of one or more numbers.

Usage

math.max(x, ...)

Parameters

NameTypeRequiredDescription
...numberYesOne or more numbers.

Returns

  • number

Example

print(math.max(3, 7, 2)) -- 7

math.min

function

math.min(x, ...)

Smallest of the given numbers.

Returns the argument with the minimum value, among a list of one or more numbers.

Usage

math.min(x, ...)

Parameters

NameTypeRequiredDescription
...numberYesOne or more numbers.

Returns

  • number

Example

print(math.min(3, 7, 2)) -- 2

math.random

function

math.random([m [, n]])

Pseudo-random number.

With no arguments, returns a float in [0, 1). With one integer argument m, returns an integer in [1, m]. With two integer arguments m, n, returns an integer in [m, n].

Usage

math.random(m, n)

Parameters

NameTypeRequiredDescription
mintegerNoLower bound (or upper bound when n is omitted).
nintegerNoUpper bound.

Returns

  • number

Example

print(math.random()) -- e.g. 0.6046602879796
print(math.random(6)) -- integer in [1, 6]
print(math.random(10, 20)) -- integer in [10, 20]

math.randomseed

function

math.randomseed(x)

Seed the pseudo-random generator.

Sets x as the seed for the pseudo-random generator used by math.random.

Usage

math.randomseed(x)

Parameters

NameTypeRequiredDescription
xnumberYesSeed value.

Returns

  • none

Example

math.randomseed(os.time())

math.sin

function

math.sin(x)

Sine.

Returns the sine of x (radians).

Usage

math.sin(x)

Parameters

NameTypeRequiredDescription
xnumberYesAngle in radians.

Returns

  • number

Example

print(math.sin(0)) -- 0

math.cos

function

math.cos(x)

Cosine.

Returns the cosine of x (radians).

Usage

math.cos(x)

Parameters

NameTypeRequiredDescription
xnumberYesAngle in radians.

Returns

  • number

Example

print(math.cos(0)) -- 1

math.tan

function

math.tan(x)

Tangent.

Returns the tangent of x (radians).

Usage

math.tan(x)

Parameters

NameTypeRequiredDescription
xnumberYesAngle in radians.

Returns

  • number

Example

print(math.tan(0)) -- 0

math.asin

function

math.asin(x)

Arc sine.

Returns the arc sine of x (in radians).

Usage

math.asin(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.asin(1)) -- 1.5707963267949

math.acos

function

math.acos(x)

Arc cosine.

Returns the arc cosine of x (in radians).

Usage

math.acos(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.acos(1)) -- 0

math.atan

function

math.atan(x)

Arc tangent.

Returns the arc tangent of x (in radians).

Usage

math.atan(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.atan(1)) -- 0.78539816339745

math.atan2

function

math.atan2(y, x)

Arc tangent of y/x.

Returns the arc tangent of y/x (in radians), using the signs of both arguments to determine the quadrant.

Usage

math.atan2(y, x)

Parameters

NameTypeRequiredDescription
ynumberYesNumerator.
xnumberYesDenominator.

Returns

  • number

Example

print(math.atan2(1, 1)) -- 0.78539816339745

math.deg

function

math.deg(x)

Radians to degrees.

Converts angle x from radians to degrees.

Usage

math.deg(x)

Parameters

NameTypeRequiredDescription
xnumberYesAngle in radians.

Returns

  • number

Example

print(math.deg(math.pi)) -- 180

math.rad

function

math.rad(x)

Degrees to radians.

Converts angle x from degrees to radians.

Usage

math.rad(x)

Parameters

NameTypeRequiredDescription
xnumberYesAngle in degrees.

Returns

  • number

Example

print(math.rad(180)) -- 3.1415926535898

math.sinh

function

math.sinh(x)

Hyperbolic sine.

Returns the hyperbolic sine of x.

Usage

math.sinh(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.sinh(0)) -- 0.0

math.cosh

function

math.cosh(x)

Hyperbolic cosine.

Returns the hyperbolic cosine of x.

Usage

math.cosh(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.cosh(0)) -- 1.0

math.tanh

function

math.tanh(x)

Hyperbolic tangent.

Returns the hyperbolic tangent of x.

Usage

math.tanh(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • number

Example

print(math.tanh(0)) -- 0.0

math.frexp

function

math.frexp(x)

Split a number into mantissa and exponent.

Returns m (a multiplier, |m| in [0.5, 1)) and e (an integer exponent) such that x = m * 2^e. Used for portable, precision-safe manipulation of floating-point values.

Usage

math.frexp(x)

Parameters

NameTypeRequiredDescription
xnumberYesInput value.

Returns

  • m (mantissa), e (exponent)

Example

local M, E = math.frexp(8)
print(M, E) -- 0.5 4

math.ldexp

function

math.ldexp(m, e)

Build a number from a mantissa and exponent.

Returns m * 2^e. The inverse of math.frexp.

Usage

math.ldexp(m, e)

Parameters

NameTypeRequiredDescription
mnumberYesMantissa.
eintegerYesExponent.

Returns

  • number

Example

print(math.ldexp(0.5, 4)) -- 8