math
The powerful math library.
Modules Fields Name Type Description enumber A variable containing the mathematical constant e. [2.7182818284590] ln2number A variable containing the mathematical constant natural logarithm of 2. [0.69314718055995] nannumber A variable containing number "not a number". [nan] pinumber A variable containing the mathematical π constant. [3.1415926535898] hugenumber A variable that effectively represents infinity, in the sense that in any numerical comparison every number will be less than this. [inf] tinynumber A variable that effectively represents negative infinity, in the sense that in any numerical comparison every number will be greater than this. [-inf] ln10number A variable containing the mathematical constant natural logarithm of 10. (2.3025850929940) log10enumber A variable containing the mathematical constant logarithm of 10 to the base e. (0.43429448190325) log2enumber A variable containing the mathematical constant logarithm of 2 to the base e. (1.4426950408889) sqrt2number A variable containing the mathematical constant square root of 2. (1.4142135623731) sqrt1_2number A variable containing the mathematical constant square root of 1/2. (0.70710678118655) randomseednumber A variable containing the current random seed and can be changed to set a new seed.
Functions math.angleCalculates the angle between two points. math.angleDifferenceReturns the difference between two angles. math.angleNormalizeReturns the normalised angle between two points. math.approachGradually approaches the target value by the specified amount. math.atanReturns the arc tangent of y/x. math.averageReturns the arithmetic mean of the given numbers. math.bucketizeReturns a function that bucketizes a number. math.cbrtReturns the cube root of the given number. math.clampClamps a number between a minimum and maximum value. math.copySignReturns x with the same sign as y. math.cotReturns the cotangent of the given angle. math.directionCalculates the direction between two points. math.euclideanModCalculates the euclidean modulus. math.fdivReturns the floor division of two numbers. math.frexpReturns m and e such that x = m2e, e is an integer and the absolute value of m is in the range ((0.5, 1) (or zero when… math.hypotReturns the square root of the sum of squares of its arguments. math.ilerpPerforms an inverse linear interpolation from the start number to the end number. math.inRangeChecks if a number is in a range. math.isDivideableChecks if a number is divisible by another number without remainder. math.isEvenChecks if a number is even. math.isFiniteChecks if a number is finite. math.isFloatChecks if a number is a float point number. math.isInfChecks if a number is positive or negative infinity. math.isIntegerChecks if a number is an signed integer. math.isNaNChecks if a number is NaN. math.isNegativeChecks if a number is negative. math.isOddChecks if a number is odd. math.isPositiveChecks if a number is positive. math.isPowerOfTwoCheck if a value is a power-of-two. math.isUnsignedIntegerChecks if a number is an unsigned integer. math.ldexpTakes a normalised number and returns the floating point representation. math.lenReturns the length of the integer. math.lerpPerforms a linear interpolation from the start number to the end number. math.log1pReturns the natural logarithm of the given number. math.lowerReturns x if its absolute value is equal to or greater than size, otherwise returns 0. math.magnitudeCalculates the magnitude (distance) between two points. math.randomfReturns a random floating point number in the range [a, b). math.rangeNormalizes a start_position/end_position pair against length, same rules as string.sub. math.relativeTranslate a relative position, negative means back from end. math.remapRemaps a number from one range to another. math.rootReturns the root of a given number with a given base. math.roundRounds the given value to the nearest whole number or to the given decimal places. math.smoothPerforms a smooth interpolation from the start number to the end number. math.snapReturns the smallest integer greater than or equal to the given number. math.thresholdCheck if value is equal or greater than threshold. math.toFloat32Converts a number to a float32. math.toIntConverts an signed integer with a sign to an integer. math.toInt32Converts a number to a 32-bit signed integer. math.toleranceCheck if value is equal or less than threshold. math.toUIntConverts an integer with a sign to an unsigned integer. math.toUInt32Converts a number to a 32-bit unsigned integer. math.truncReturns the integer part of the given number, rounding towards zero. math.upperReturns x if its absolute value is equal to or less than size, otherwise returns 0. math.wrapWraps a number to keep it within a specific minimum and maximum range.
function math.angle (x1 : number , y1 : number , x2 : number , y2 : number ): number
Calculates the angle between two points.
Parameters
Name Type Description x1number The x coordinate of the first point. y1number The y coordinate of the first point. x2number The x coordinate of the second point. y2number The y coordinate of the second point.
Returns
Type Name Description number angleThe angle between the two points.
function math.angleDifference (a : number , b : number ): number
Returns the difference between two angles.
Parameters
Name Type Description anumber The first angle. bnumber The second angle.
Returns
Type Name Description number diffThe difference between the angles.
function math.angleNormalize (angle : number ): number
Returns the normalised angle between two points.
Parameters
Name Type Description anglenumber The angle to normalise.
Returns
Type Name Description number normalisedThe normalised angle.
function math.approach (current : number , target : number , change : number ): number
Gradually approaches the target value by the specified amount.
Parameters
Name Type Description currentnumber The current value. targetnumber The target value. changenumber The amount that the current value is allowed to change by to approach the target.
Returns
Type Name Description number approachedThe approached value.
function math.atan (y : number , x : number ): number
Returns the arc tangent of y/x.
Parameters
Name Type Description ynumber The y coordinate. xnumber The x coordinate.
Returns
Type Name Description number arc_tanThe arc tangent of y/x.
function math.average (... : number ): number
Returns the arithmetic mean of the given numbers.
Parameters
Name Type Description ...number The numbers to average.
Returns
Type Name Description number averageThe arithmetic mean of the given numbers.
function math.bucketize (y : T): fun ( x: number ): T
Returns a function that bucketizes a number.
Parameters
Name Type Description yTThe bucket size.
Returns
Type Name Description fun ( x: number ): TbucketizerThe bucketizer function.
function math.cbrt (x : number ): number
Returns the cube root of the given number.
Parameters
Name Type Description xnumber The number to calculate the cube root of.
Returns
Type Name Description number cubeThe cube root of the number.
function math.clamp (number : number , min : number , max : number ): number
Clamps a number between a minimum and maximum value.
Parameters
Name Type Description numbernumber The number to clamp. minnumber The minimum value. maxnumber The maximum value.
Returns
Type Name Description number clampedThe clamped number.
function math.copySign (x : T, y : number ): T
Returns x with the same sign as y.
Parameters
Name Type Description xTThe number to copy the sign of. ynumber The number to get the sign from.
Returns
Type Name Description TnumberThe number with the sign of y.
function math.cot (x : number ): number
Returns the cotangent of the given angle.
Parameters
Name Type Description xnumber The angle in radians.
Returns
Type Name Description number cotangentThe cotangent of the given angle.
function math.direction (x1 : number , y1 : number , x2 : number , y2 : number ): number , number
Calculates the direction between two points.
Parameters
Name Type Description x1number The x coordinate of the first point. y1number The y coordinate of the first point. x2number The x coordinate of the second point. y2number The y coordinate of the second point.
Returns
Type Name Description number xThe x coordinate of the direction. number yThe y coordinate of the direction.
function math.euclideanMod (numerator : number , denominator : number ): number
Calculates the euclidean modulus.
Parameters
Name Type Description numeratornumber The numerator. denominatornumber The denominator.
Returns
Type Name Description number modThe euclidean modulus.
function math.fdiv (a : T, b : T): T
Returns the floor division of two numbers.
Parameters
Name Type Description aTThe first number. bTThe second number.
Returns
Type Name Description TxThe floor division of the two numbers.
function math.frexp (x : number ): number , number
Returns m and e such that x = m2e, e is an integer and the absolute value of m is in the range ((0.5, 1) (or zero when x is zero).
Used to split the number value into a normalized fraction and an exponent. Two values are returned: the first is a multiplier in the range 1/2 (inclusive) to 1 (exclusive) and the second is an integer exponent.
The result is such that x = m*2^e.
Parameters
Name Type Description xnumber The number to split.
Returns
Type Name Description number mThe normalized fraction. number eThe exponent.
function math.hypot (numbers : number[] , number_count? : integer ): number
Returns the square root of the sum of squares of its arguments.
Parameters
Name Type Description numbersnumber[] The numbers to calculate the square root of. number_countoptional integer The number of numbers to calculate the square root of.
Returns
Type Name Description number valueThe square root of the sum of squares of its arguments.
function math.ilerp (result : number , from : number , to : number ): number
Performs an inverse linear interpolation from the start number to the end number.
Parameters
Name Type Description resultnumber The interpolated value. fromnumber The start number. tonumber The end number.
Returns
Type Name Description number lerpedThe fraction of the way between the start and end numbers.
function math.inRange (number : number , from : number , to : number ): boolean
Checks if a number is in a range.
Parameters
Name Type Description numbernumber The number to check. fromnumber The minimum value of the range. tonumber The maximum value of the range.
Returns
Type Name Description boolean in_rangetrue if the number is in the range, otherwise false.
function math.isDivideable (a : number , b : number ): boolean
Checks if a number is divisible by another number without remainder.
Parameters
Name Type Description anumber The first number to check. bnumber The second number to check.
Returns
Type Name Description boolean is_divideabletrue if the first number is divisible by the second number, otherwise false.
function math.isEven (x : number ): boolean
Checks if a number is even.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_eventrue if the number is even, otherwise false.
function math.isFinite (x : number ): boolean
Checks if a number is finite.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_finitetrue if the number is finite, otherwise false.
function math.isFloat (x : number ): boolean
Checks if a number is a float point number.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_floattrue if the number is a float, otherwise false.
function math.isInf (x : number ): boolean
Checks if a number is positive or negative infinity.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_inftrue if the number is positive or negative infinity, otherwise false.
function math.isInteger (x : number ): boolean
Checks if a number is an signed integer.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_inttrue if the number is an integer, otherwise false.
function math.isNaN (x : number ): boolean
Checks if a number is NaN.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_nantrue if the number is NaN, otherwise false.
function math.isNegative (x : number ): boolean
Checks if a number is negative.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_negativetrue if the number is negative, otherwise false.
function math.isOdd (x : number ): boolean
Checks if a number is odd.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_oddtrue if the number is odd, otherwise false.
function math.isPositive (x : number ): boolean
Checks if a number is positive.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_positivetrue if the number is positive, otherwise false.
function math.isPowerOfTwo (x : number ): boolean
Check if a value is a power-of-two.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_pottrue if a number is a valid power-of-two, otherwise false.
function math.isUnsignedInteger (x : number ): boolean
Checks if a number is an unsigned integer.
Parameters
Name Type Description xnumber The number to check.
Returns
Type Name Description boolean is_uinttrue if the number is an integer, otherwise false.
function math.ldexp (x : number , exponent : number ): number
Takes a normalised number and returns the floating point representation.
Effectively it returns the result of normalizedFraction * 2.0 ^ exponent.
Parameters
Name Type Description xnumber The base value. exponentnumber The exponent.
Returns
Type Name Description number floatThe floating point representation.
See also
function math.len (x : integer ): integer
Returns the length of the integer.
Parameters
Name Type Description xinteger The integer to check.
Returns
Type Name Description integer lengthThe length of the integer.
function math.lerp (fraction : number , from : number , to : number ): number
Performs a linear interpolation from the start number to the end number.
Parameters
Name Type Description fractionnumber The fraction of the way between the start and end numbers. fromnumber The start number. tonumber The end number.
Returns
Type Name Description number lerpedThe interpolated value.
function math.log1p (x : number ): number
Returns the natural logarithm of the given number.
Parameters
Name Type Description xnumber The number to calculate the logarithm of.
Returns
Type Name Description number logThe natural logarithm of the number.
function math.lower (x : number , size : number ): number
Returns x if its absolute value is equal to or greater than size, otherwise returns 0.
Parameters
Name Type Description xnumber The number to check. sizenumber The minimum allowed absolute value before snapping to 0.
Returns
Type Name Description number resultThe original number, or 0.
function math.magnitude (x1 : number , y1 : number , x2 : number , y2 : number ): number
Calculates the magnitude (distance) between two points.
Parameters
Name Type Description x1number The x coordinate of the first point. y1number The y coordinate of the first point. x2number The x coordinate of the second point. y2number The y coordinate of the second point.
Returns
Type Name Description number magnitudeThe magnitude between the two points.
function math.randomf (a : number , b : number ): number
Returns a random floating point number in the range [a, b).
Parameters
Name Type Description anumber The minimum value. bnumber The maximum value.
Returns
Type Name Description number floatThe random floating point number.
function math.range (length : integer , start_position? : integer , end_position? : integer ): integer , integer
Normalizes a start_position/end_position pair against length, same rules as string.sub.
Negative positions count back from the end, start_position is clamped to >= 1 and end_position to [0, length].
An empty range is returned as start_position > end_position.
Parameters
Name Type Description lengthinteger The length of the sequence. start_positionoptional integer The start position, defaults to 1. end_positionoptional integer The end position, defaults to length.
Returns
Type Name Description integer start_positionThe normalized start position. integer end_positionThe normalized end position.
function math.relative (position : integer , length : integer , fallback? : integer ): integer
Translate a relative position, negative means back from end.
Parameters
Name Type Description positioninteger The position to check. lengthinteger The length to check. fallbackoptional integer The fallback position.
Returns
Type Name Description integer relativeThe relative position.
function math.remap (number : number , inMin : number , inMax : number , outMin : number , outMax : number ): number
Remaps a number from one range to another.
Parameters
Name Type Description numbernumber The number to remap. inMinnumber The minimum value of the input range. inMaxnumber The maximum value of the input range. outMinnumber The minimum value of the output range. outMaxnumber The maximum value of the output range.
Returns
Type Name Description number valueThe remapped value.
function math.root (a : number , b : number ): number
Returns the root of a given number with a given base.
Parameters
Name Type Description anumber The number to calculate the root of. bnumber The base of the root.
Returns
Type Name Description number rootThe root of the number.
function math.round (number : number , decimals? : integer ): number
Rounds the given value to the nearest whole number or to the given decimal places.
Parameters
Name Type Description numbernumber The number to round. decimalsoptional integer The number of decimal places to round to.
Returns
Type Name Description number roundedThe rounded number.
function math.smooth (previous : number , next : number , alpha : number ): number
Performs a smooth interpolation from the start number to the end number.
Parameters
Name Type Description previousnumber The previous value. nextnumber The next value. alphanumber The amount of smoothing.
Returns
Type Name Description number valueThe interpolated value.
function math.snap (number : number , step : number ): number
Returns the smallest integer greater than or equal to the given number.
Parameters
Name Type Description numbernumber The number to round. stepnumber The step size to round to.
Returns
Type Name Description number snappedThe rounded number.
function math.threshold (x : number , threshold? : number ): boolean
Check if value is equal or greater than threshold.
NOTE : This is useful to mitigate accuracy issues in floating point numbers .
Examples:
lua Copy
local a = 10
local b = 13.3
if math.threshold ( a - b, 4 ) then
print ( "yes" )
else
print ( "no" )
end
-- print: `no` because difference between a and b is not so far from 4
Parameters
Name Type Description xnumber The number to check. thresholdoptional number The maximum difference between numbers at which they are considered equal; by default 1e-8.
Returns
Type Name Description boolean is_greater_equaltrue if the numbers are equal or greater, otherwise false.
function math.toFloat32 (number : number ): number
Converts a number to a float32.
Parameters
Name Type Description numbernumber The number to convert.
Returns
Type Name Description number floatThe float32 number.
function math.toInt (x : T, bit_count : integer ): T
Converts an signed integer with a sign to an integer.
Parameters
Name Type Description xTThe number to convert. bit_countinteger The bit count of the signed integer.
Returns
Type Name Description TsignedThe integer with a sign.
function math.toInt32 (x : T): T
Converts a number to a 32-bit signed integer.
Parameters
Name Type Description xTThe number to convert.
Returns
Type Name Description TsignedThe 32-bit signed integer.
function math.tolerance (x : number , threshold? : number ): boolean
Check if value is equal or less than threshold.
NOTE : This is useful to mitigate accuracy issues in floating point numbers .
Examples:
lua Copy
local a = 10
local b = 13.3
if math.tolerance ( a - b, 4 ) then
print ( "yes" )
else
print ( "no" )
end
-- print: `yes` because difference between a and b is near of 4
Parameters
Name Type Description xnumber The number to check. thresholdoptional number The maximum difference between numbers at which they are considered equal; by default 1e-8.
Returns
Type Name Description boolean is_nearly_equaltrue if the numbers are equal or less, otherwise false.
function math.toUInt (x : T, bit_count : integer ): T
Converts an integer with a sign to an unsigned integer.
Parameters
Name Type Description xTThe number to convert. bit_countinteger The bit count of the unsigned integer.
Returns
Type Name Description TunsignedThe unsigned integer.
function math.toUInt32 (x : T): T
Converts a number to a 32-bit unsigned integer.
Parameters
Name Type Description xTThe number to convert.
Returns
Type Name Description TunsignedThe 32-bit unsigned integer.
function math.trunc (x : number ): integer
Returns the integer part of the given number, rounding towards zero.
NOTE : Same result as (math.modf( x )), but math.modf is ~10x slower under JIT.
Parameters
Name Type Description xnumber The number to truncate.
Returns
Type Name Description integer truncatedThe integer part of the number.
function math.upper (x : number , size : number ): number
Returns x if its absolute value is equal to or less than size, otherwise returns 0.
Useful for ignoring extreme spikes or outliers in values.
Parameters
Name Type Description xnumber The number to check. sizenumber The maximum allowed absolute value before snapping to 0.
Returns
Type Name Description number resultThe original number, or 0.
function math.wrap (value : number , min : number , max : number ): number
Wraps a number to keep it within a specific minimum and maximum range.
Parameters
Name Type Description valuenumber The number to wrap. minnumber The lower limit of the wrap range. maxnumber The upper limit of the wrap range.
Returns
Type Name Description number wrappedThe number wrapped within [min, max).