math
The math library is a standard Lua library that provides functions for manipulating numbers. In Garry's Mod several additional math functions have been added.
Fields
number math.huge
A variable that effectively represents infinity, in the sense that in any numerical comparison every number will be less than this.
For example, if x is a number, x > math. huge will NEVER be true except in the case of overflow (see below).
Lua will consider any number greater than or equal to 2^1024 (the exponent limit of a double) as inf and hence equal to this.
Methods
number math.abs( number x )
Calculates the absolute value of a number (effectively removes any negative sign).
number math.Approach( number current, number target, number change )
Gradually approaches the target value by the specified amount.
number math.ApproachAngle( number currentAngle, number targetAngle, number rate )
Increments an angle towards another by specified rate.
This function is for numbers representing angles (0-360), NOT Angle objects!
number math.atan2( number y, number x )
functions like math. atan(y / x), except it also takes into account the quadrant of the angle and so doesn't have a limited range of output.
The Y argument comes first!
number math.calcBSplineN( number i, number k, number t, number tinc )
This is used internally - although you're able to use it you probably shouldn't.
Use math. BSplinePoint instead.
Basic code for Bezier-Spline algorithm, helper function for math. BSplinePoint.
number math.Clamp( number input, number min, number max )
Clamps a number between a minimum and maximum value.
number math.Dist( number x1, number y1, number x2, number y2 )
We advise against using this. It may be changed or removed in a future update.
You should use math. Distance instead
Returns the difference between two points in 2D space. Alias of math. Distance.
number math.Distance( number x1, number y1, number x2, number y2 )
Returns the difference between two points in 2D space.
number math.DistanceSqr( number x1, number y1, number x2, number y2 )
Returns the squared difference between two points in 2D space. This is computationally faster than math. Distance.
number math.ease.InBack( number fraction )
Eases in by reversing the direction of the ease slightly before returning.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.InBounce( number fraction )
Eases in like a bouncy ball.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.InElastic( number fraction )
Eases in like a rubber band.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.InExpo( number fraction )
Eases in using an exponential equation with a base of 2 and where the fraction is used in the exponent.
number math.ease.InOutBack( number fraction )
Eases in and out by reversing the direction of the ease slightly before returning on both ends.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.InOutBounce( number fraction )
Eases in and out like a bouncy ball.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.InOutElastic( number fraction )
Eases in and out like a rubber band.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.InOutExpo( number fraction )
Eases in and out using an exponential equation with a base of 2 and where the fraction is used in the exponent.
number math.ease.InOutQuart( number fraction )
Eases in and out by raising the fraction to the power of 4.
number math.ease.InOutQuint( number fraction )
Eases in and out by raising the fraction to the power of 5.
number math.ease.OutBack( number fraction )
Eases out by reversing the direction of the ease slightly before finishing.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.OutBounce( number fraction )
Eases out like a bouncy ball.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.OutElastic( number fraction )
Eases out like a rubber band.
This doesn't work properly when used with Lerp as it clamps the fraction between 0 and 1. Using your own version of Lerp that is unclamped would be necessary instead.
number math.ease.OutExpo( number fraction )
Eases out using an exponential equation with a base of 2 and where the fraction is used in the exponent.
number math.EaseInOut( number progress, number easeIn, number easeOut )
Calculates the progress of a value fraction, taking in to account given easing fractions
number math.fmod( number base, number modulator )
Returns the modulus of the specified values.
While this is similar to the % operator, it will return a negative value if the first argument is negative, whereas the % operator will return a positive value even if the first operand is negative.
number, number math.frexp( number x )
Lua reference description: 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.
number math.ldexp( number normalizedFraction, number exponent )
Takes a normalised number and returns the floating point representation.
number math.log( number x, number base = e )
With one argument, return the natural logarithm of x (to base e).
With two arguments, return the logarithm of x to the given base, calculated as log(x)/log(base).
number math.log10( number x )
Returns the base-10 logarithm of x. This is usually more accurate than math.log(x, 10).
number math.mod( number base, number modulator )
We advise against using this. It may be changed or removed in a future update.
This is removed in Lua versions later than what GMod is currently using. You should use the % operator or math.fmod instead.
Returns the modulus of the specified values. Same as math.fmod.
number, number math.modf( number base )
Returns the integral and fractional component of the modulo operation.
number math.NormalizeAngle( number angle )
Normalizes angle, so it returns value between -180 and 180.
number math.pow( number x, number y )
Returns x raised to the power y.
In particular, math.pow(1.0, x) and math.pow(x, 0.0) always return 1.0, even when x is a zero or a nan. If both x and y are finite, x is negative, and y is not an integer then math.pow(x, y) is undefined.
number math.Rand( number min, number max )
Returns a random float between min and max.
See also math. random
number math.random( number m = nil, number n = nil )
When called without arguments, returns a uniform pseudo-random real number in the range 0 to 1 which includes 0 but excludes 1.
When called with an integer number m, returns a uniform pseudo-random integer in the range 1 to m inclusive.
When called with two integer numbers m and n, returns a uniform pseudo-random integer in the range m to n inclusive.
See also math.Rand
math.randomseed( number seed )
Seeds the random number generator. The same seed will guarantee the same sequence of numbers each time with math.random.
For shared random values across predicted realms, use util.SharedRandom.
Incorrect usage of this function will affect all random numbers in the game.
number math.Remap( number value, number inMin, number inMax, number outMin, number outMax )
Remaps the value from one range to another
number math.Round( number value, number decimals = 0 )
Rounds the given value to the nearest whole number or to the given decimal places.
number math.SnapTo( number input, number snapTo )
Snaps a number to the closest multiplicative of given number. See also Angle:SnapTo.
number math.TimeFraction( number start, number end, number current )
Returns the fraction of where the current time is relative to the start and end times