Programming & Scripting Tutorials

Lua: Advanced Math





To do more advanced math in lua, you can use math functions.
Here is a list of most(if not all) of the math functions (some of which we will go over today):

math.abs
math.acos
math.asin
math.atan
math.atan2
math.ceil
math.cos
math.cosh
math.deg
math.exp
math.floor
math.fmod
math.frexp
math.huge
math.ldexp
math.log
math.log10
math.max
math.min
math.modf
math.pi
math.pow
math.rad
ath.random
math.randomseed
math.sin
math.sinh
math.sqrt
math.tanh
math.tan

Ok, so lets start going through some of them..

math.abs



This simply gives the absolute (non-negative) value of a number.
For example -100 to 100:

math.abs(-100)


math.ceil and math.floor



These two are all about rounding.
math.ceil round UP (you can remember this as its like ceiling and the ceiling is up), for example the math.ceil of 3.3:

math.ceil(3.3)

The above would round to 4, as it always rounds up.
math.floor is a lot like ceil, but instead of being the ceiling and rounding up, its the floor. Therefore, yes you guessed it, it rounds down.
The math.floor of 3.8 for example would go to 3:

math.floor(3.8)


math.pow



math.pow raises the first number to the power of the second number.
For example the three to the power of four:

math.pow(3, 4)


-
Have a little play round with the others math functions, some of them are more advanced mathmatical calculations, most of them being pretty obvious from the name what they do; I didn't want to explain them in this lesson as it might confuse the less mathmatically inclined.


This Lua tutorial was written by


Back to Lua

Advertisement: