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
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.
A variable containing the mathematical constant pi. (3. 1415926535898)
See also: Trigonometry
It should be noted that due to the nature of floating point numbers, results of calculations with math. pi may not be what you expect. See second example below.
A variable containing the mathematical constant tau, which is equivalent to 2*math. pi. (6. 28318530718)
See also: Trigonometry
It should be noted that due to the nature of floating point numbers, results of calculations with math. tau may not be what you expect. See the second example on math. pi page.
Methods
Calculates the absolute value of a number (effectively removes any negative sign).
Calculates the difference between two angles.
Gradually approaches the target value by the specified amount.
Increments an angle towards another by specified rate.
This function is for numbers representing angles (0-360), NOT Angle objects!
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!
Basic code for Bézier-Spline algorithm.
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.
Cubic Hermite spline algorithm.
Clamps a number between a minimum and maximum value.
Lerp point between 4 control points with cubic bezier.
See math. QuadraticBezier for a similar function which works with 3 control points.
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.
Returns the difference between two points in 2D space.
Returns the squared difference between two points in 2D space. This is computationally faster than math. Distance.
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.
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.
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.
Eases in using an exponential equation with a base of 2 and where the fraction is used in the exponent.
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.
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.
Eases in and out by cubing the 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.
Eases in and out using an exponential equation with a base of 2 and where the fraction is used in the exponent.
Eases in and out by raising the fraction to the power of 4.
Eases in and out by raising the fraction to the power of 5.
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.
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.
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.
Eases out using an exponential equation with a base of 2 and where the fraction is used in the exponent.
Calculates the progress of a value fraction, taking in to account given easing fractions
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.
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.
Takes a normalised number and returns the floating point representation.
Effectively it returns the result of normalizedFraction * 2. 0 ^ exponent. math. frexp is the opposite function.
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).
Returns the base-10 logarithm of x. This is usually more accurate than math. log(x, 10).
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.
Returns the integral and fractional component of the modulo operation.
Normalizes angle, so it returns value between -180 and 180.
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.
Lerp point between 3 control points with quadratic bezier.
See math. CubicBezier for a function which works with 4 control points.
Returns a random float between min and max.
See also math. random
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.
Remaps the value from one range to another
Rounds the given value to the nearest whole number or to the given decimal places.
Snaps a number to the closest multiplicative of given number. See also Angle:SnapTo.
Returns the fraction of where the current time is relative to the start and end times