Garry's Mod Wiki

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.
number math.pi
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.
number math.tau
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

number math.abs( number x )
Calculates the absolute value of a number (effectively removes any negative sign).
number math.acos( number cos )
Returns the arccosine of the given number.
Calculates the difference between two angles.
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.asin( number normal )
Returns the arcsine of the given number.
number math.atan( number normal )
Returns the arctangent of the given number.
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.BinToInt( string string )
Converts a binary string into a number.
Vector math.BSplinePoint( number fraction, table points, number fractionMax )
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.
number math.ceil( number number )
Ceils or rounds a number up.
Vector math.CHSpline( number frac, Vector point0, Vector tan0, Vector point1, Vector tan1 )
Cubic Hermite spline algorithm.
number math.Clamp( number input, number min, number max )
Clamps a number between a minimum and maximum value.
number math.cos( number number )
Returns the cosine of given angle.
number math.cosh( number number )
Returns the hyperbolic cosine of the given angle.
Vector math.CubicBezier( number frac, Vector p0, Vector p1, Vector p2, Vector p3 )
Lerp point between 4 control points with cubic bezier. See math. QuadraticBezier for a similar function which works with 3 control points.
number math.deg( number radians )
Converts radians to degrees.
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.InCirc( number fraction )
Eases in using a circular function.
number math.ease.InCubic( number fraction )
Eases in by cubing the fraction.
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.InOutCirc( number fraction )
Eases in and out using a circular function.
number math.ease.InOutCubic( number fraction )
Eases in and out by cubing the fraction.
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.InOutQuad( number fraction )
Eases in and out by squaring the fraction.
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.InOutSine( number fraction )
Eases in and out using math. sin.
number math.ease.InQuad( number fraction )
Eases in by squaring the fraction.
number math.ease.InQuart( number fraction )
Eases in by raising the fraction to the power of 4.
number math.ease.InQuint( number fraction )
Eases in by raising the fraction to the power of 5.
number math.ease.InSine( number fraction )
Eases in using math. sin.
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.OutCirc( number fraction )
Eases out using a circular function.
number math.ease.OutCubic( number fraction )
Eases out by cubing the fraction.
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.ease.OutQuad( number fraction )
Eases out by squaring the fraction.
number math.ease.OutQuart( number fraction )
Eases out by raising the fraction to the power of 4.
number math.ease.OutQuint( number fraction )
Eases out by raising the fraction to the power of 5.
number math.ease.OutSine( number fraction )
Eases out using math. sin.
number math.EaseInOut( number progress, number easeIn = 0, number easeOut = 1 )
Calculates the progress of a value fraction, taking in to account given easing fractions
number math.exp( number exponent )
Returns the x power of the Euler constant.
number math.Factorial( number val_in )
Simple function that calculates factorial of a whole number.
number math.floor( number number )
Floors or rounds a number down.
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.
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.
string math.IntToBin( number int )
Converts an integer to a binary (base-2) string.
number math.ldexp( number normalizedFraction, number exponent )
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.
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.max( vararg numbers )
Returns the largest value of all arguments.
number math.min( vararg numbers )
Returns the smallest value of all arguments.
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.
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.
Vector math.QuadraticBezier( number frac, Vector p0, Vector p1, Vector p2 )
Lerp point between 3 control points with quadratic bezier. See math. CubicBezier for a function which works with 4 control points.
number math.rad( number degrees )
Converts an angle in degrees to it's equivalent in radians.
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.sin( number number )
Returns the sine of given angle.
number math.sinh( number number )
Returns the hyperbolic sine of the given angle.
number math.SnapTo( number input, number snapTo )
Snaps a number to the closest multiplicative of given number. See also Angle:SnapTo.
number math.sqrt( number value )
Returns the square root of the number.
number math.tan( number value )
Returns the tangent of the given angle.
number math.tanh( number number )
Returns the hyperbolic tangent of the given number.
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
number math.Truncate( number num, number digits = 0 )
Trim unwanted decimal places.