Mathematics in Lua
Lesson 2
Today, we are going to go a bit more into detail about what you can do with variables, especially number variables and how you can use them for mathematics in Lua.
As we learnt in the previous lesson, one of the most important data types is numbers. Numbers can do a lot of stuff in Lua, both basic and complicated stuff. So let’s start with the basic stuff. We’ll open Roblox Studio, go on a Baseplate and create a script. Then we’ll create two variables that we will use along the course.
number1 = 4
number2 = 8
Now that we have two variables, we can start learning about how mathematics works in Lua. What we can do with the two numbers is perform different operations. Operations are very important when scripting and they are being performed using different symbols:
Addition (+):
print(number1 + number2)
if we run the script, the output would be this:
As you can see, 12 has been printed (4+8). Keep in mind that you can do this with any numbers!
Subtraction (-):
print(number1 - number2)
Can you guess what the output will be now?
As you can see, -4 has been printed (the result of 4-8).
Of course, we could also subtract the first number from the second number like this:
print(number2 - number1)
Now our output is no longer a negative number (8-4)
Multiplication (*):
print(number1 * number2)
Please remember that the multiplication symbol in Lua is *. Let’s see what our output is this time.
As you can see, 32 has been printed (4*8).
Division (/):
print(number1 / number2)
Remember that the division symbol used in Lua is /. The output is the following:
The output is 0.5 (4/ 8). Of course, we could also divide number1 from number2 like this:
print(number2 / number1)
and here's the output:
Modulus (%):
print(number1 % number2)
Keep in mind that the remainder of the division is the amount left over after a division. This is going to be the output:
Of course, we could also do this. In this case, there will be no remainder since 4 is a divisor of 8.
print(number2 % number1)
The Output:
Exponent (^):
print(number1 ^ number2)
Please remember that the exponent symbol in Lua is ^. Let’s take a look at the output:
As you can see, the result was a pretty big number. Let’s see what result we get if we raise number2 at the power of number1, like this:
print(number2 ^ number1)
The number this time wasn’t that big.
We could experiment with even bigger numbers to see what the results are.
Negate (-number):
Reverses the sign of a number
print(-number1)
print(-number2)
The output would be:
-4
-8
Both numbers' signs were inverted (from + to −). That, of course, would also work from - to +. Try it for yourself!
Summery
These are all the 7 operations that can be done using symbols in Lua. Let’s make a recap quickly:
Addition (+): Adds two numbers together (number1 + number2).
Subtraction (-): Removes the second number from the first number (number1 - number2).
Multiplication (*): Multiplies two numbers (number1 * number2).
Division (/): Divides the first number from the second number (number1 / number2).
Modulus (%): Gets the remainder of a division of two numbers (number1 % number2)
Exponent (^): Raises the first number to the power of the second number (number1 ^ number2)
Negate (-number): Reverses the sign of a number (-number).
Now that we know about all the symbols in Lua, let’s talk about the order of operations. The order of operations in Lua isn’t very hard to understand, as it doesn’t differ at all from what you learnt in school:
Parentheses
Exponents
Divisions/Multiplications
Additions/Subtractions
Parentheses in Lua are very easy to understand. Their symbol is ( and ). For example, the following line of code uses parentheses:
x = 121/11+(10*2+(17+47))
print(x)
Can you calculate the result?
You can use as many parentheses as you like; just remember to close them!
Tips & Tricks (T&T)
Let’s say you want to double a variable, like this:
number2 = number2 * 2
There is a faster way to write that code by doing this:
number2 *= 2
The same trick applies to all the other operations.
number2 += 2
number2 -= 2
number2 *= 2
number2 /= 2
number2 ^= 2
number2 %= 2
You can also do this with variables:
number2 += number1
the same thing as:
number2 = number2 + number1
It’s time to talk about more complex mathematics in Lua. To use Complex Mathematics in Lua, you need to access the math library.
To do that, type math. like this:
As you can see, multiple functions popped up. By clicking on each of them, you get their descriptions. You can try them and see the results!
Here are the most commonly used math library functions in Lua:
abs(number): Returns the absolute value of a number.
sqrt(number): Returns the square root of a number.
ceil(number): Rounds a number up to the next largest integer.
floor(number): Rounds a number to the largest integer smaller than or equal to a number.
math.pi - Returns the value of pi (~3.14).
math.huge - Returns infinite (inf).
math.min(x,y, …..) - Returns the smallest value.
math.max(x,y, …..) - Returns the largest value.
math.random(x,y) - Returns a random value in the range of [x,y].
Last updated
Was this helpful?