-
Notifications
You must be signed in to change notification settings - Fork 7
Lua Coding Docs: Library Methods ‐ Mathematical Methods
Finds the minimum number by searching through each argument passed until it finds one.
-
multi
- An infinite parameter, The multiple number arguments to find the minimum number.
Examples:
debugPrint( math.min(43, 34, 19, 34) ) --> 19
debugPrint( math.min(1.34, 0.37, 94.34) ) --> 0.37
debugPrint( math.min(0x83, 0x8a, 0xff) ) --> 131
debugPrint( math.min(-43, -34, -19, -34) ) --> -43
debugPrint( math.min(-1.34, -0.37, -94.34) ) --> -94.34
debugPrint( math.min(-0x83, -0x8a, -0xff) ) --> -255
Finds the maximum number by searching through each argument passed until it finds one.
-
multi
- An infinite parameter, The multiple number arguments to find the maximum number.
Example:
debugPrint( math.max(43, 34, 19, 34) ) --> 43
debugPrint( math.max(1.34, 0.37, 94.34) ) --> 94.34
debugPrint( math.max(0x83, 0x8a, 0xff) ) --> 255
debugPrint( math.max(-43, -34, -19, -34) ) --> -19
debugPrint( math.max(-1.34, -0.37, -94.34) ) --> -0.37
debugPrint( math.max(-0x83, -0x8a, -0xff) ) --> -131
Rounds down a floating-point number to its nearest integer value.
-
num
- The number to round down closest integer number.
Example:
debugPrint( math.floor(93.73) ) --> 93
debugPrint( math.floor(54.23) ) --> 54
Rounds up a floating-point number to its nearest integer value.
-
num
- The number to round up closest integer number.
Example:
debugPrint( math.floor(93.73) ) --> 94
debugPrint( math.floor(54.23) ) --> 55
The absolute value from a given number, distance of the number from
-
num
- The number to returns its absolute number.
Example:
debugPrint( math.abs(-47) ) --> 47
debugPrint( math.abs(47) ) --> 47
The square root of the given number.
-
num
- The radicand of the given number.
Example:
debugPrint( math.sqrt(4) ) --> 2
debugPrint( math.sqrt(2) ) --> 1.4142135623731
Returns the floating-point integral part (integer) and fractional part (decimal places).
-
num
- The floating-point to split into integral and fractional parts.
Example:
local int, frc = math.modf(364.36452)
debugPrint(int) --> 364
debugPrint(frc) --> 0.36452
The logarithmic of a number to the base, inverse (opposite) of exponential. Which makes the base number is the exponent must be raised to produced the number. For an example, since
Caution
Logarithms cannot have negative numbers and the base must be above
-
num
- The logarithmic of the given number to its base. -
base
- An optional parameter, The logarithmic base number; Default value:2
.
Example:
debugPrint( math.log(32, 2) ) --> 5.0
debugPrint( math.log(13, 2) ) --> 3.7004397181411
The logarithmic of a number to the base of
-
num
- The logarithmic of the given number to the base ten$10$ .
Example:
debugPrint( math.log10(1000) ) --> 3.0
debugPrint( math.log10(567) ) --> 2.7535830588929
Randomizes a number between the minimum to maximum numbers. It utilizes a Pseudo-Random Number Generator (PRNG), an mathematical algorithm that takes use of formulas. To produce a sequences of random numbers; utilizing a seed to initializing the PRNG.
Since it's not truly "random", due to computer limitations. It will always return the exact same number sequences, even if you try restarting it will still generate the exact same number sequences. That's because it uses the same seed that the PRNG utilizes within that script.
Tip
If you want to generate an "actually" random number, you can either both use. The getRandomInt()
function for integer numbers and getRandomFloat()
function for floating-point numbers. Or use the math.randomseed()
method to change its current seed to generate different random number sequences.
-
min
- An optional parameter, the minimum number value range to start randomizing; Default value:0
. -
max
- An optional parameter, the maximum number value range to end randomizing; Default value:1
.
Example:
debugPrint( math.random() ) --> 0.84018771676347
debugPrint( math.random(1, 6) ) --> 5
Sets the seed of the pseudo-random number generator; equal seeds produce equal sequences of numbers.
Tip
Highly recommended to use os.time()
method, this returns every second from the Unix Epoch Time, which initially starts at: 00:00:00 UTC on 1 January 1970
. Making the seed change every second thus making it actually random.
-
seed
- The specified seed to generate a new random number sequence.
Examples:
math.randomseed(103)
debugPrint( math.random(4, 6) ) --> 5
debugPrint( math.random(1, 40) ) --> 37
math.randomseed(os.time())
debugPrint( math.random(4, 6) ) --> 4
debugPrint( math.random(1, 40) ) --> 23
Converts a radians to degrees.
-
deg
- The degree number to convert to radians.
Example:
debugPrint( math.rad(90) ) --> 1.5707963267949 or (1/2)π
debugPrint( math.rad(180) ) --> 3.1415926535898 or π
debugPrint( math.rad(30) ) --> 0.5235987755983 or (1/6)π
Converts a degrees to radians.
-
rad
- The radian number to convert to degrees.
Example:
debugPrint( math.deg( math.pi/2 ) ) --> 90.0
debugPrint( math.deg( 6*math.pi/6 ) ) --> 180.0
debugPrint( math.deg( 8*math.pi/6 ) ) --> 240.0
Returns the sine of the number, given in radians.
-
rad
- The radians to calculate the sine value.
Example:
debugPrint(math.sin(180)) --> -0.80115263573383
debugPrint(math.sin(360)) --> 0.95891572341431
debugPrint(math.sin(2 * math.pi)) --> -2.4492935982947e-16
Returns the cosine of the number, given in radians.
-
rad
- The radians to calculate the cosine value.
Example:
debugPrint(math.cos(180)) --> -0.59846006905786
debugPrint(math.cos(360)) --> -0.28369109148653
debugPrint(math.cos(2 * math.pi)) --> 1.0
Returns the tangent of the number, given in radians.
-
rad
- The radians to calculate the tangent value.
Example:
debugPrint(math.tan(180)) --> 1.3386902103512
debugPrint(math.tan(360)) --> -3.380140413961
debugPrint(math.tan(2 * math.pi)) --> -2.4492935982947e-16
Returns the arc (inverse) sine of the number, given in radians. Returns between
-
rad
- The radians to calculate the inverse sine value; Goes from-1
to1
.
Example:
debugPrint( math.asin(-1) ) --> -1.5707963267949 or -(1/2)π
debugPrint( math.asin(0) ) --> 0.0
debugPrint( math.asin(1) ) --> 1.5707963267949 or (1/2)π
Returns the arc (inverse) cosine of the number, given in radians. Returns between
-
rad
- The radians to calculate the inverse cosine value; Goes from-1
to1
.
Example:
debugPrint( math.acos(-1) ) --> 3.1415926535898 or π
debugPrint( math.acos(0) ) --> 1.5707963267949 or π/2
debugPrint( math.acos(1) ) --> 0.0
Returns the arc (inverse) tangent of the number, given in radians. Returns between
-
rad
- The radians to calculate the inverse tangent value.
Example:
debugPrint( math.atan(-1) ) --> -0.78539816339745 or -(π/4)
debugPrint( math.atan(0) ) --> 0.0
debugPrint( math.atan(1) ) --> 0.78539816339745 or π/4
Returns the calculated arc (inverse) tangent of x and y coordinate points, given in radians. Between the between the positive x-axis and the ray from the origin to the x and y coordinate points in the Cartesian plane.
-
y
- The given y position to calculate. -
x
- The given x position to calculate.
Example:
Calculates the given x and y coordinate plane degrees.
local calcDeg = function() -- i stole this example
return (math.atan2(y, x) * 180) / math.pi
end
debugPrint( calcDeg(10, 10) ) --> 45.0
debugPrint( calcDeg(180, 0) ) --> 90.0
debugPrint( calcDeg(270, 180) ) --> 56.30993247402
Returns the hyperbolic sine of the number, given in radians.
-
rad
- The radians to calculate the hyperbolic sine value.
Example:
debugPrint( math.sinh(1) ) --> 1.1752011936438
debugPrint( math.sinh(0.3)) --> 0.30452029344714
debugPrint( math.sinh(-5) ) --> -74.203210577789
Returns the hyperbolic cosine of the number, given in radians.
-
rad
- The radians to calculate the hyperbolic cosine value.
Example:
debugPrint( math.cosh(1) ) --> 1.5430806348152
debugPrint( math.cosh(0.3) ) --> 1.0453385141289
debugPrint( math.cosh(-5) ) --> 74.209948524788
Returns the hyperbolic tangent of the number, given in radians.
-
rad
- The radians to calculate the hyperbolic tangent value.
Example:
debugPrint( math.tanh(1) ) --> 0.76159415595576
debugPrint( math.tanh(0.3) ) --> 0.29131261245159
debugPrint( math.tanh(-5) ) --> -0.9999092042626
The constant value of pi 3.1415926535898
.
The constant value of "infinity", any number that is greater than or equal to inf
.
Is the page in some way inaccurate? an error, a typo, or outdated data? To report it, use the "Issue Tab". Or do you wish to include a new function or add new information? use the "Pull Request Tab". Help is always appreciated!
- Event Callbacks
- Custom Sprite
- Custom Text
- Object Functions
- General Functions
- Scripting & File Functions
- Game Input Control Functions
- Language Translation
- HScript Functions
- Custom Substates
- Custom Shaders
- Deprecated & Removed Functions
- Sound & Music Functions
- Tweens & Timers Functions
- Reflection Functions
- Variables