In Lua, numbers are one of the 8 data types
available.
With the number data type, we can represent
numerical data and do mathematical operations,
like addition and subtraction.
Using the number data type is easy and straightforward,
and we'll take a look at how you can do that
in this tutorial.
First, we need some numbers to work with,
so let’s assign numbers to two variables.
To assign a number to a variable, just set
the variable to equal the number with no quotes.
So I'll create a variable called amount, and
set it to equal 10.
I'll create another variable, called bonus,
and set it to 2.
Last, I'll create a third variable called
result, where we’ll do some math on the
amount and bonus variables.
To start, let’s add amount plus bonus.
I'll edit the print function to display the
result variable, and run the script, which
gives us the result, 12.
This plus sign, is called an operator.
An operator is a symbol that tells the interpreter
to perform a specific mathematical or logical
manipulation.
Here, the operator tells Lua to add the two
values surrounding it together.
We can change the operator to a minus sign,
to subtract the bonus from the amount, and
we get the result of 8.
The multiplication operator is the asterisk,
so if I replace the minus sign with the asterisk,
the result is two times ten, giving us 20.
You can perform division with the forward
slash, and 10 divided by 2 gives us a result
of 5.
There are two other operators we can use,
modulus and exponent.
You can use the modulus operator to get the
remainder from any division operations.
Let’s change bonus to equal 3 so I can show
you what I mean.
Three goes into ten three times, with a remainder
of 1.
If we were to use the division operator, we
would get a result of 3.
But, you can get the remainder using the modulus
operator, which is the percent sign.
Run the script, and we get the remainder,
1.
The last mathematical operator we can use
is exponent.
The exponent operator allows us to calculate
exponents, such as 3 to the 5th power.
If we replace the percent sign with the caret,
the result is 10 to the 3rd power, which is
1000.
These mathematical operations aren’t limited
to two numbers, either.
We can add more numbers together by setting
the result to equal amount plus bonus, which
is 10 plus 3, plus 7.
If we run the script, the gives us the result
of 20.
We could change one of these addition operators
to subtraction, and the script will return
the result of 10 plus 3 minus 7, which is
6.
Once you start building longer, more complex
statements, you have to take into consideration
operator precedence.
Operator precedence is the order that the
operators will be evaluated in.
At first, you may think that math would be
handled from left to right, in this case 10
plus three, which equals 13, then 13 minus
7, which gives us 6.
But, depending on the operators you are using,
some parts of the statement may be evaluated
before others.
For example, let’s change the subtraction
operator, to the multiplication operator.
Multiplication operators will be evaluated
before addition operators.
If we evaluated this line from left to right,
we would get 10 plus 3, 13, multiplied by
7, which would be 91.
But, since multiplication is evaluated before
addition, we have to start with 3 multiplied
by 7, which is 21, and then we add that to
10, which gives us 31.
If we run the script you can see, the result
is 31.
In Lua, the operator precedence, or the order
in which operators will be evaluated, is multiplication
first, then division, then modulus.
After that, addition, and then subtraction
will be performed.
It’s important to remember the operator
precedence, because you may get unexpected
results if you don’t think about when each
operator will be evaluated.
Let’s say you had this line of code, but
you didn’t want the multiplication to be
evaluated first.
You can control the order of operations, by
wrapping the code you want to execute first
in parenthesis.
So, I’ll wrap amount plus bonus in parenthesis,
which will tell Lua to evaluate this first,
and then continue on with the rest of the
statement following the order of operations.
If I run the script, we get 91, which means
the addition was evaluated first.
If you have an even longer statement, you
can nest parenthesis inside each other.
Let’s say we add 3 after the amount plus
bonus, and we run the script, we get the result
of 34.
If we wanted the plus three to be evaluated
before it’s multiplied by 7, we could wrap
parenthesis around the first parenthesis and
plus three.
This is called nesting.
In this case, the nested parenthesis, the
ones inside the new parenthesis, are calculated
first, then the next set of parenthesis up
are evaluated, in this case adding 3.
Finally, it’s multiplied by 7, which gives
us the result, 112.
In the past, all numbers in Lua were floats,
and there were no true integers.
Integers are just whole numbers, and floats
are numbers with a decimal point.
What this means is that if you had a variable
that was equal to 1, it was actually equal
to 1.0.
Every number had a decimal.
But in Lua 5.3, the integer subtype was introduced.
Now, we have the Number data type which is
the parent, while integer and float are both
subtypes of the parent, Number.
That means Lua will evaluate both a float
and an integer’s data type as number.
But, it will convert between the two as necessary
in your scripts.
All of the numbers we have used so far have
been integers, but we can change them to be
a float by placing a decimal followed by the
decimal places.
So, if we change bonus to 2.50, we have converted
it to a float.
Amount is still an integer, but if we add
amount plus bonus, the result will be an float,
since we have .50 left over.
