Library math

Lua's (not glua's) math library, plus clamp, lerp, remap, round, randfloat and some easing functions.

Functions

abs( value ) Returns the absolute value of the given number (i.e. turns negative values positive).
acos( value ) Returns the inverse cosine of the given value.
asin( value ) Returns the inverse sine of the given value.
atan( value ) Returns the inverse tangent of the given value.
atan2( y, x ) Returns the angle of the line from (0, 0) to (x, y), in radians.
ceil( value ) Returns the given value rounded up to the smallest integer that is greater than it.
clamp( value, min, max ) Clamps the given number between the given minimum and maximum value.
cos( value ) Returns the cosine of the given value.
cosh( value ) Returns the hyperbolic cosine of the given value.
deg( radians ) Returns the given value in radians converted into degrees.
easeInBack( fraction ) Eases in by reversing the direction of the ease slightly before returning.
easeInBounce( fraction ) Eases in like a bouncy ball.
easeInCirc( fraction ) Eases in using a circular function.
easeInCubic( fraction ) Eases in by cubing the fraction.
easeInElastic( fraction ) Eases in like a rubber band.
easeInExpo( fraction ) Eases in using an exponential equation with a base of 2 and where the fraction is used in the exponent.
easeInOutBack( fraction ) Eases in and out by reversing the direction of the ease slightly before returning on both ends.
easeInOutBounce( fraction ) Eases in and out like a bouncy ball.
easeInOutCirc( fraction ) Eases in and out using a circular function.
easeInOutCubic( fraction ) Eases in and out by cubing the fraction.
easeInOutElastic( fraction ) Eases in and out like a rubber band.
easeInOutExpo( fraction ) Eases in and out using an exponential equation with a base of 2 and where the fraction is used in the exponent.
easeInOutQuad( fraction ) Eases in and out by squaring the fraction.
easeInOutQuart( fraction ) Eases in and out by raising the fraction to the power of 4.
easeInOutQuint( fraction ) Eases in and out by raising the fraction to the power of 5.
easeInOutSine( fraction ) Eases in and out using math.sin.
easeInQuad( fraction ) Eases in by squaring the fraction.
easeInQuart( fraction ) Eases in by raising the fraction to the power of 4.
easeInQuint( fraction ) Eases in by raising the fraction to the power of 5.
easeInSine( fraction ) Eases in using math.sin.
easeOutBack( fraction ) Eases out by reversing the direction of the ease slightly before finishing.
easeOutBounce( fraction ) Eases out like a bouncy ball.
easeOutCirc( fraction ) Eases out using a circular function.
easeOutCubic( fraction ) Eases out by cubing the fraction.
easeOutElastic( fraction ) Eases out like a rubber band.
easeOutQuart( fraction ) Eases out by raising the fraction to the power of 4.
easeOutQuint( fraction ) Eases out by raising the fraction to the power of 5.
easeOutSine( fraction ) Eases out using math.sin.
exp( power ) Returns the euler constant raised to the given power.
floor( value ) Returns the given value rounded down to the largest integer that is smaller than it.
fmod( value, modulus ) Returns the modulus of the given value.
frexp( the ) Returns the multiplier and exponent that correspond to the given value.
ldexp( multiplier, exponent ) Returns the value corresponding with the given multiplier and exponent (effectively reversing math.frexp).
lerp( fraction, lowerBound, upperBound ) Returns the linear interpolation between the given lower and upper bound at the given fraction.
log( value, base ) Returns the logarithm of the given value, using the given base.
log10( value ) Returns the base-10 logarithm of the given value.
max( ... ) Returns the maximum value taken from the given arguments.
min( ... ) Returns the minimum value taken from the given arguments.
mod( value, modulus ) Returns the modulus of the given value.
modf( value ) Returns the integer and fractional parts of the given value.
pow( base, exponent ) Returns the given value raised to the given power (equivalent to the '^' operator).
rad( degrees ) Returns the given value in degrees converted into radians.
randfloat( min, max ) Returns a random floating point value between the given minimum and maximum values.
random( lowerBound, upperBound ) Returns a random value in the range [0, 1) if given no arguments, or otherwise a random integer between the given bounds.
remap( value, fromLowerBound, fromUpperBound, toLowerBound, toUpperBound ) Returns the given value mapped from the first number range into the second number range.
round( value, decimals ) Rounds the given number to the given number of decimal places (rounding half-up).
sin( value ) Returns the sine of the given value.
sinh( value ) Returns the hyperbolic sine of the given value.
sqrt( value ) Returns the square root of the given value.
tan( value ) Returns the tangent of the given value.
tanh( value ) Returns the hyperbolic tangent of the given value.

Fields

huge Represents the floating point value for "infinity".
pi Represents the constant pi.


Functions

math.abs( value )
Returns the absolute value of the given number (i.e. turns negative values positive).

Environment:

Shared

Parameters:

  • (number) value: The value to get the absolute value of.
math.acos( value )
Returns the inverse cosine of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the inverse cosine of.
math.asin( value )
Returns the inverse sine of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the inverse sine of.
math.atan( value )
Returns the inverse tangent of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the inverse tangent of.
math.atan2( y, x )
Returns the angle of the line from (0, 0) to (x, y), in radians. This can be thought of in terms of a right-angled triangle, where x is the adjacent side, and y is the opposite side.

Environment:

Shared

Parameters:

  • (number) y: The y co-ordinate (opposite triangle side length).
  • (number) x: The x co-ordinate (adjacent triangle side length).
math.ceil( value )
Returns the given value rounded up to the smallest integer that is greater than it.

Environment:

Shared

Parameters:

  • (number) value: The value to get the ceiling of.
math.clamp( value, min, max )
Clamps the given number between the given minimum and maximum value.

Environment:

Shared

Parameters:

  • (number) value: The value to be clamped.
  • (number) min: The minimum value to clamp the value to.
  • (number) max: The maximum value to clamp the value to.

Return value:

    (number) The value clamped between the given minimum and maximum value.
math.cos( value )
Returns the cosine of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the cosine of (expressed in radians).
math.cosh( value )
Returns the hyperbolic cosine of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the hyperbolic consine of.
math.deg( radians )
Returns the given value in radians converted into degrees.

Environment:

Shared

Parameters:

  • (number) radians: The value in radians to convert to degrees.
math.easeInBack( fraction )
Eases in by reversing the direction of the ease slightly before returning.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInBounce( fraction )
Eases in like a bouncy ball.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInCirc( fraction )
Eases in using a circular function.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInCubic( fraction )
Eases in by cubing the fraction.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInElastic( fraction )
Eases in like a rubber band.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInExpo( fraction )
Eases in using an exponential equation with a base of 2 and where the fraction is used in the exponent.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutBack( fraction )
Eases in and out by reversing the direction of the ease slightly before returning on both ends.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutBounce( fraction )
Eases in and out like a bouncy ball.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutCirc( fraction )
Eases in and out using a circular function.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutCubic( fraction )
Eases in and out by cubing the fraction.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutElastic( fraction )
Eases in and out like a rubber band.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutExpo( fraction )
Eases in and out using an exponential equation with a base of 2 and where the fraction is used in the exponent.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutQuad( fraction )
Eases in and out by squaring the fraction.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutQuart( fraction )
Eases in and out by raising the fraction to the power of 4.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutQuint( fraction )
Eases in and out by raising the fraction to the power of 5.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInOutSine( fraction )
Eases in and out using math.sin.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInQuad( fraction )
Eases in by squaring the fraction.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInQuart( fraction )
Eases in by raising the fraction to the power of 4.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInQuint( fraction )
Eases in by raising the fraction to the power of 5.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeInSine( fraction )
Eases in using math.sin.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutBack( fraction )
Eases out by reversing the direction of the ease slightly before finishing.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutBounce( fraction )
Eases out like a bouncy ball.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutCirc( fraction )
Eases out using a circular function.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutCubic( fraction )
Eases out by cubing the fraction.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutElastic( fraction )
Eases out like a rubber band.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutQuart( fraction )
Eases out by raising the fraction to the power of 4.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutQuint( fraction )
Eases out by raising the fraction to the power of 5.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.easeOutSine( fraction )
Eases out using math.sin.

Environment:

Shared

Parameters:

  • (number) fraction: Value to ease (between 0 and 1).

Return value:

    (number) The eased value that can be passed into a lerping function (e.g. math.lerp).
math.exp( power )
Returns the euler constant raised to the given power.

Environment:

Shared

Parameters:

  • (number) power: The power of e to return.
math.floor( value )
Returns the given value rounded down to the largest integer that is smaller than it.

Environment:

Shared

Parameters:

  • (number) value: The value to get the floor of.
math.fmod( value, modulus )
Returns the modulus of the given value. Unlike the '%' operator, this returns negative values for a negative number.

Environment:

Shared

Parameters:

  • (number) value: The value to apply the modulus to.
  • (number) modulus: The modulus to apply.
math.frexp( the )
Returns the multiplier and exponent that correspond to the given value. The multiplier and exponent represent the given value as follows: value = mult * 2 ^ exponent.

Environment:

Shared

Parameters:

  • (number) the: number to get the multiplier and exponent for.

Return values:

  1. (number) The multiplier (between 0.5 and 1).
  2. (number) The exponent (always an integer).
math.ldexp( multiplier, exponent )
Returns the value corresponding with the given multiplier and exponent (effectively reversing math.frexp).

Environment:

Shared

Parameters:

  • (number) multiplier: The multiplier (e.g. from math.frexp).
  • (number) exponent: The exponent (e.g. from math.frexp).
math.lerp( fraction, lowerBound, upperBound )
Returns the linear interpolation between the given lower and upper bound at the given fraction.

Environment:

Shared

Parameters:

  • (number) fraction: The fraction between the lower and upper bound to return (normally between 0 and 1, though values outside this range are permitted).
  • (number) lowerBound: The lower bound to return when the given fraction = 0.
  • (number) upperBound: The upper bound to return when the given fraction = 1.
math.log( value, base )
Returns the logarithm of the given value, using the given base. Consider using math.log10 for base-10 logarithms, as it has greater precision.

Environment:

Shared

Parameters:

  • (number) value: The value to get the logarithm for.
  • (number?) base: An optional base to use for the logarithm (default = math.exp( 1 )).
math.log10( value )
Returns the base-10 logarithm of the given value. This is more accurate than math.log( value, 10 ) as it uses a more precise numerical method.

Environment:

Shared

Parameters:

  • (number) value: The value to get the base-10 logarithm for.
math.max( ... )
Returns the maximum value taken from the given arguments.

Environment:

Shared

Parameters:

  • (number...) ...: The values to get the maximum value from.
math.min( ... )
Returns the minimum value taken from the given arguments.

Environment:

Shared

Parameters:

  • (number...) ...: The values to get the minimum value from.
math.mod( value, modulus )
Returns the modulus of the given value. Unlike the '%' operator, this returns negative values for a negative number.

Deprecated: Use the '%' operator, or math.fmod.

Environment:

Shared

Parameters:

  • (number) value: The value to apply the modulus to.
  • (number) modulus: The modulus to apply.
math.modf( value )
Returns the integer and fractional parts of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to extract the integer and fractional parts from.

Return values:

  1. (number) The integer part of the given value.
  2. (number) The fractional part of the given value.
math.pow( base, exponent )
Returns the given value raised to the given power (equivalent to the '^' operator).

Environment:

Shared

Parameters:

  • (number) base: The base value.
  • (number) exponent: The exponent.
math.rad( degrees )
Returns the given value in degrees converted into radians.

Environment:

Shared

Parameters:

  • (number) degrees: The value in degrees to convert to radians.
math.randfloat( min, max )
Returns a random floating point value between the given minimum and maximum values.

Environment:

Shared

Parameters:

  • (number) min: The minimum value.
  • (number) max: The maximum value.

Return value:

    (number) A random floating point number between the given minimum and maximum value.
math.random( lowerBound, upperBound )
Returns a random value in the range [0, 1) if given no arguments, or otherwise a random integer between the given bounds.

Environment:

Shared

Parameters:

  • (number?) lowerBound: The lower bound of the random values produced (or the upper bound if the only argument).
  • (number?) upperBound: The upper bound of the random values produced.
math.remap( value, fromLowerBound, fromUpperBound, toLowerBound, toUpperBound )
Returns the given value mapped from the first number range into the second number range.

Environment:

Shared

Parameters:

  • (number) value: The value to be mapped.
  • (number) fromLowerBound: The lower bound of the range the value is currently within.
  • (number) fromUpperBound: The upper bound of the range the value is currently within.
  • (number) toLowerBound: The lower bound of the range the value should be mapped into.
  • (number) toUpperBound: The upper bound of the range the value should be mapped into.

Usage:

    -- Maps a value from a percentage range into a fractional range (the result here is 0.5). 
    local Fraction = math.remap( 50, 0, 100, 0, 1 )
math.round( value, decimals )
Rounds the given number to the given number of decimal places (rounding half-up).

Environment:

Shared

Parameters:

  • (number) value: The value to be rounded.
  • (number?) decimals: (Optional) The number of decimals to round to (default = 0).

Return value:

    (number) The value rounded to the given number of decimals.
math.sin( value )
Returns the sine of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the sine of (expressed in radians).
math.sinh( value )
Returns the hyperbolic sine of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the hyperbolic sine of.
math.sqrt( value )
Returns the square root of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the square root of.
math.tan( value )
Returns the tangent of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the tangent of (expressed in radians).
math.tanh( value )
Returns the hyperbolic tangent of the given value.

Environment:

Shared

Parameters:

  • (number) value: The value to get the hyperbolic tangent of.

Valid XHTML 1.0!