Garry's Mod Wiki



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.


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.
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.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.
number math.AngleDifference( number a, number b )
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 tDiff, table tPoints, number tMax )
Basic code for algorithm.
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.ceil( number number )
Ceils or rounds a number up.
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.
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.EaseInOut( number progress, number easeIn, number easeOut )
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.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.
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.
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.
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.
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.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.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 )
Rounds towards zero.

Special Pages



Render Time: 77ms

DB GetPage 45
Generate Html 2
SaveChanges (1) 10
Render Body 0
Render Sidebar 16