Library
math
A library of math functions.
About math
This library is an interface to the standard C math library, providing all of
its functions inside the math table.
Properties 7
e
: numberThe value of Euler's number, e.huge
: numberReturns a value larger than or equal to any other numerical value (about 21024).Returns a value larger than or equal to any other numerical value (about 21024). Dividing a positive number by zero yields this same value.
nan
: numberA NaN value, as defined by the IEEE 754 standard.A NaN value, as defined by the IEEE 754 standard. Comparing directly to
math.nanwill always return false; usemath.isnaninstead.phi
: numberThe value of the golden ratio.pi
: numberThe value of pi.sqrt2
: numberThe value of the square root of 2.tau
: numberThe value of tau, which is defined as2 * math.pi.
Functions 37
abs
(x: number) → numberReturns the absolute value ofx.acos
(x: number) → numberReturns the arc cosine ofx.asin
(x: number) → numberReturns the arc sine ofx.atan
(x: number) → numberReturns the arc tangent ofxin radians.atan2
(y: number, x: number) → numberReturns the arc tangent ofy/x(in radians) while using the signs of both parameters to find the quadrant of the result.Returns the arc tangent of
y/x(in radians) while using the signs of both parameters to find the quadrant of the result. It also handles correctly the case ofxbeing zero.ceil
(x: number) → intReturns the smallest integer larger than or equal tox.clamp
(x: number, min: number, max: number) → numberReturns a number betweenminandmax, inclusive.cos
(x: number) → numberReturns the cosine ofx, assumed to be in radians.cosh
(x: number) → numberReturns the hyperbolic cosine ofx.deg
(x: number) → numberReturns the anglex(given in radians) in degrees.exp
(x: number) → numberReturns the valuee^x.floor
(x: number) → intReturns the largest integer smaller than or equal tox.fmod
(x: number, y: number) → numberReturns the remainder of the division ofxbyythat rounds the quotient towards zero.frexp
(x: number) → number, intReturnsmandesuch thatx=m*2^e.Returns
mandesuch thatx=m*2^e.eis an integer and the absolute value ofmis in the range of0.5to1(inclusive of0.5but exclusive of1), or zero whenxis zero.isfinite
(x: number) → booleanReturns true ifxis a finite number.Returns true if
xis a finite number, meaning it is neither NaN nor positive or negative infinity (±math.huge).isinf
(x: number) → booleanReturns true ifxis positive or negative infinity and false otherwise.Returns true if
xis positive or negative infinity (±math.huge) and false otherwise.isnan
(x: number) → booleanReturns true ifxis not a number (NaN) and false otherwise.ldexp
(x: number, e: int) → numberReturnsx*2^e(eshould be an integer).lerp
(a: number, b: number, t: number) → numberReturns the linear interpolation betweenaandb.Returns the linear interpolation between
aandbbased on the factort.This function uses the formula
a+(b-a)*t.tis typically between0and1but values outside this range are acceptable.log
(x: number, base: number = 2.7182818) → numberReturns the logarithm ofxusing the given base.Returns the logarithm of
xusing the given base, or the mathematical constanteif no base is provided (natural logarithm).log10
(x: number) → numberReturns the base-10 logarithm ofx.map
(x: number, inmin: number, inmax: number, outmin: number, outmax: number) → numberReturns the value ofxmapped from one range to another.Returns a value that represents
xmapped linearly from the input range (inmintoinmax) to the output range (outmintooutmax). This is achieved by determining the relative position ofxwithin the input range and applying that ratio to the output range.max
(x: number, ...: number) → numberReturns the maximum value among the numbers passed to the function.min
(x: number, ...: number) → numberReturns the minimum value among the numbers passed to the function.modf
(x: number) → number, numberReturns two numbers: the integral part ofxand the fractional part ofx.noise
(x: number, y: number = 0, z: number = 0) → numberReturns a Perlin noise value.Returns a Perlin noise value. The returned value is most often between the range of
-1to1(inclusive) but sometimes may be outside that range; if the interval is critical to you, useLibrary.math.clamp(noise, -1, 1)on the output.If you leave arguments out, they will be interpreted as zero, so
Library.math.noise(1.158)is equivalent toLibrary.math.noise(1.158, 0, 0)andLibrary.math.noise(1.158, 5.723)is equivalent toLibrary.math.noise(1.158, 5.723, 0).Note that this function uses a Perlin noise algorithm to assign fixed values to coordinates. For example,
Library.math.noise(1.158, 5.723)will always return0.48397532105446andLibrary.math.noise(1.158, 6)will always return0.15315161645412.If
x,y, andzare all integers, the return value will be0. For fractional values ofx,y, andz, the return value will gradually fluctuate between-0.5and0.5. For coordinates that are close to each other, the return values will also be close to each other.The noise repeats with a period of
256on each axis, soLibrary.math.noise(x, y, z)andLibrary.math.noise(x + 256, y, z)return identical values.pow
(x: number, y: number) → numberReturnsx^y.Returns
x^y(you can also use the expressionx^yto compute this value).rad
(x: number) → numberReturns the anglex(given in degrees) in radians.random
(m: number = 0, n: number = 1) → numberReturns a random number within the range provided.When called without arguments, returns a uniform pseudo-random real number in the range of
0to1(inclusive of0but exclusive of1).When called with an integer number
m, returns a uniform pseudo-random integer in the range of1tom, inclusive.When called with two integer numbers
mandn, returns a uniform pseudo-random integer in the range ofmton, inclusive.Internally, this uses a 32-bit PCG (Permuted Congruential Generator) which achieves excellent statistical performance and makes its output hard to predict.
randomseed
(x: number) → ()Setsxas the seed for the pseudo-random generator.Sets
xas the seed for the pseudo-random generator: equal seeds produce equal sequences of numbers.round
(x: number) → numberReturns the integer with the smallest difference between it and the given number.Returns the integer with the smallest difference between it and the given number. For example, the value
5.8returns6.For values like
0.5that are equidistant to two integers, the value with the greater difference between it and zero is chosen. In other words, the function "rounds away from zero" such that0.5rounds to1and-0.5rounds to-1.sign
(x: number) → intReturns-1ifxis less than0,0ifxequals0, or1ifxis greater than0.sin
(x: number) → numberReturns the sine ofx, assumed to be in radians.sinh
(x: number) → numberReturns the hyperbolic sine ofx.sqrt
(x: number) → numberReturns the square root ofx.Returns the square root of
x. You can also use the expressionx^0.5to compute this value.tan
(x: number) → numberReturns the tangent ofx, assumed to be in radians.tanh
(x: number) → numberReturns the hyperbolic tangent ofx.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.