Skip to content

Math library

Provides mathematical functions and constants. This library includes trigonometric, exponential, logarithmic, and other standard math operations, along with constants such as π.

Constants

math.huge

A special value representing positive infinity.

math.maxinteger

The maximum value that can be represented by an integer in Lua.

math.mininteger

The minimum value that can be represented by an integer in Lua.

math.pi

The value of π (pi), the ratio of a circle's circumference to its diameter.

Functions

math.abs(number)

Computes the absolute value of number.

Parameters:

  • number
    A numberic value.

Return values
The absolute value of number.

math.acos(number)

Returns the arc cosine of number.

Parameters:

  • number
    A number between -1 and 1.

Return values
The arc cosine of the input number, in radians.

math.asin(number)

Returns the arc sine of number.

Parameters:

  • number
    A number between -1 and 1.

Return values
The arc sine of the input number, in radians.

math.atan(number)

Returns the arc tangent of a number, in radians.

Parameters:

  • number
    A numeric value representing a tangent.

Return values
The arc tangent of the input number, in radians.

math.ceil(number)

Returns the smallest integral value larger than or equal to number.

Parameters:

  • number
    A numeric value to round up.

Return values
The smallest integer greater than or equal to the given number.

math.cos(number)

Returns the cosine of `number.

Parameters:

  • number
    An angle in radians.

Return values
The cosine of the angle.

math.deg(number)

Converts an angle from radians to degrees.

Parameters:

  • number
    An angle in radians.

Return values
The angle converted to degrees.

math.exp(number)

Returns the value of the constant e raised to the power of the given number.

Parameters:

  • number
    The exponent to raise e to.

Return values
The result of e^number.

math.floor(number)

Returns the largest integral value less than or equal to the given number.

Parameters:

  • number
    A numeric value to round down.

Return values
The largest integer less than or equal to the given number.

math.fmod(divided, divisor)

Returns the remainder of the division of the first number by the second.

Parameters:

  • divided
    The dividend.

  • divisor
    The divisor.

Return values
The remainder of the division.

math.log(number [, base])

Returns the logarithm of a number in a given base. Defaults to natural logarithm if base is not provided.

Parameters:

  • number
    The number to compute the logarithm for.

  • base
    The logarithmic base. Optional; defaults to e.

math.max(number, ...)

Returns the largest value among its arguments.

Parameters:

  • number
    The first number to compare.

  • ...
    Additional numbers to compare.

math.min(number, ...)

Returns the smallest value among its arguments.

Parameters:

  • number
    The first number to compare.

  • ...
    Additional numbers to compare.

math.modf(number)

Splits a number into its integer and fractional parts.

Parameters:

  • number
    The number to split.

Return values
Two numbers: the integer part and the fractional part.

math.rad()

Converts an angle from degrees to radians.

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

Generates a pseudo-random number. The behavior depends on the provided arguments.

Parameters:

  • m
    The lower limit of the random number range (optional).

  • n
    The upper limit of the random number range (optional).

math.randomseed()

Sets the seed for the random number generator.

math.sin()

Returns the sine of an angle (in radians).

math.sqrt()

Returns the square root of a number.

math.tan()

Returns the tangent of an angle (in radians).

math.tointeger()

Converts a number to an integer if it is exactly representable.

math.type()

Returns the type of a number: 'integer', 'float', or nil if not a number.

math.ult()

Compares two unsigned integers and returns true if the first is less than the second.