> For the complete documentation index, see [llms.txt](https://base.einspire.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://base.einspire.net/programming/lua/mathematics-in-lua.md).

# Mathematics in Lua

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.&#x20;

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.

```lua
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 (+)**:&#x20;

```
print(number1 + number2)
```

if we run the script, the output would be this:

![output](https://lh3.googleusercontent.com/i2857LWT6VzNiq8QhiNhhKzBdKVgG5U69CxG1Fmu3_JwKRiTwt0zsVmhgRK_VdWVwd61FhWS9xLBl1L5D2dH_Fq-nMDpmUXy2zvzHLK8X-XdhU3QCdmrrBNqkUxgUl-FIhAS-kyK=s0)

As you can see, **12** has been printed (4+8). Keep in mind that you can do this with any **numbers**!

## &#x20;**Subtraction (-)**:

```lua
print(number1 - number2)
```

&#x20;Can you guess what the output will be now?

![output](https://lh5.googleusercontent.com/rXWIOObVZjAs_3BERtSA64t43bTzrNAghlJYwxXZQQZWy_wCWy0bPxruCoiTjx58zo3fphWGRqE9cQrrGaa66tax2I9lxhTEeGDi3BOTdzgH8ZzQbbOV01HfjFlRoTo2jw9DSH0_=s0)

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:&#x20;

```lua
print(number2 - number1)
```

Now our output is no longer a **negative** number (8-4)

![](https://lh4.googleusercontent.com/Zr-nFtq8yR6GpAF1pnBMaTrIYIxtTdqXlaSZz2pkHSTnwcU3eYxgFMsMXVNg8sBeO8YA80hi3yZqBonZqRO_9u-S8LtHI_5BrUXtFSbZCUYi6O7B-QbUeekKWPrB9vzOzPrsLbHC=s0)

## **Multiplication (\*)**:&#x20;

```lua
print(number1 * number2)
```

Please remember that the **multiplication** symbol in Lua is **\*.** Let’s see what our output is this time.

![output (4\*8)](https://lh5.googleusercontent.com/cOMfDTJ59BX8yRhbCatgNJ_L4q_D8T0OpvFejDoqfr0RdywNp_F0e_Fm7Hgw1DAXNaT8jIt73_DH6fSRuPxPFo1GjXHzsn5ZAycYXI0crzYZmsrvmLZtGyW-F1z7xhD-0YhguBlX=s0)

&#x20;As you can see, 32 has been printed (4\*8).

## **Division (/)**:&#x20;

```
print(number1 / number2)
```

Remember that the **division** symbol used in Lua is **/**. The output is the following:

![output (4/8)](https://lh4.googleusercontent.com/6l_zFLgFZbfD41V2eAyuPBTGT0jkIvwYOjSpEZ_dGYpHVqxfdoEaSZL4z9UElrXexYu_MZp8ETA_Pa1s4D8ogEc1PvoMWKC-KdBJvH42iaChFVTy62sOPNiGDNSXlYMlRFyYLnPt=s0)

The output is 0.5 (4/ 8). Of course, we could also divide number1 from number2 like this:

```lua
print(number2 / number1)
```

and here's the output:

![](https://lh6.googleusercontent.com/CbMhXgJqTeaMNc-3_zlP2qP-bQj8CDx_CHXXKBadrckurPArQ-UuYzMLs2JFRVB9k-S7ISetLCTvZf6ntwFeXlIaqLQSu-PBoThGoPlhcHheWDcN-vOKEkMQUivdvpov4piFTm3e=s0)

## **Modulus (%):**

```lua
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:<br>

![](https://lh5.googleusercontent.com/aZIndx7ho6mdNgq4G6OA62HqyewKe1evk-KIKQNJlSt4vmXJbr3spwZC2dfBdmcgO3OPQXFWC5MDJgzyumsBQh0GbsruoT_1e_oI4FFHHabf8_XiipJAXRmmc68o6JaD9WoaVieR=s0)

Of course, we could also do this. In this case, there will be no remainder since 4 is a divisor of 8.

```lua
print(number2 % number1)
```

The Output:

![](https://lh5.googleusercontent.com/NByLk1Daw4b1AXgVF1pitxhOAHhYL60JX11w_FS2luk_eEEDqTvjKiZQ6OepztSKDcoW_KqpUpRL-OOF-o-vn7FQyBdyDQ6U0Q4zNa_HUmZDKN9E9XHdc_z89fff2hrI8mj4EPh6=s0)

## Exponent (^):

```lua
print(number1 ^ number2)
```

&#x20; Please remember that the exponent symbol in Lua is **^**. Let’s take a look at the output:

![](https://lh3.googleusercontent.com/LWLn5arc1_WKcJGoHjam2MXPkfgi_S0EGdN3463IDQXfmt9bNRU-ASwUlPrfZVq8tySPzl8ukK3T_nQwsMfSpwMB8GqzhTeKaBdQ_OZtZwTdg7gE9iasqFNZzMQL1Y_oKMm2RTYi=s0)

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:

```lua
 print(number2 ^ number1)
```

&#x20;The number this time wasn’t that big.

![](https://lh3.googleusercontent.com/-_URt3hXEOSMHicUbOnb38QxmOQnqeWvg5Dw_rXOJISZHXY9dySBPoXkB0eeX4DOyceBAUIEj9oeTbCaX8mWtaOqpfx-VjMunqGfoYa5QOIAVs46GkdE8vonHpxjm-IFfytLU99m=s0)

&#x20; We could experiment with even bigger numbers to see what the results are.

## Negate (-number):

> **Reverses the sign of a number**

```lua
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:<br>

1. &#x20; **Addition (+):** Adds two numbers together (number1 + number2).
2. &#x20; **Subtraction (-):** Removes the second number from the first number (number1 - number2).
3. &#x20; **Multiplication (\*):** Multiplies two numbers (number1 \* number2).
4. &#x20; **Division (/):** Divides the first number from the second number (number1 / number2).
5. &#x20; **Modulus (%):** Gets the remainder of a division of two numbers (number1 % number2)
6. &#x20; **Exponent (^):** Raises the first number to the power of the second number (number1 ^ number2)
7. &#x20; **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:

1. **Parentheses**
2. **Exponents**
3. **Divisions/Multiplications**
4. **Additions/Subtractions**

Parentheses in Lua are very easy to understand. Their symbol is ( and ). For example, the following line of code uses parentheses:<br>

```lua
x = 121/11+(10*2+(17+47))
print(x)
```

Can you calculate the result?

![](https://lh3.googleusercontent.com/z3CU7M3CNkitEt2tURtc5JAFyo7xllbZIAqXrbnKnQQv1asZKxBibgAF6pM5hg01mztK9b0ZQzrxT3FuHx0Y6h1ZV1xUuIkpfHaalkdvOyL7TdrjefpP8E7QBVKKnTmM6bUj5Tjb=s0)

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 thi&#x73;**:**

```lua
number2 = number2 * 2
```

&#x20;There is a faster way to write that code by doing thi&#x73;**:**

```lua
number2 *= 2
```

The same trick applies to all the other operations.

```lua
number2 += 2 
number2 -= 2
number2 *= 2
number2 /= 2
number2 ^= 2
number2 %= 2
```

&#x20;**You can also do this with variables:**

```lua
number2 += number1
```

the same thing a&#x73;**:**

```lua
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.&#x20;

**To do that, type&#x20;*****math.*****&#x20;like this:**<br>

![](https://lh5.googleusercontent.com/cORvgfQh1ekJOY9qZtq7k4bui3VjIInSj0fWEdJ6AsyUSCyVF2dw1TOOjds7S94vfm3SjVh50NuEeJSdZyozZLL2mdIsR4ESIWBZj29-3FZTYRFOcvgvqy517KIIATD_gGF4f-oJ=s0)

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!

&#x20; Here are the **most commonly** used math library functions in Lua:

* &#x20;**abs(number):** Returns the absolute value of a number.
* &#x20;**sqrt(number):** Returns the square root of a number.
* &#x20;**ceil(number):**  Rounds a number up to the next largest integer.
* &#x20;**floor(number):** Rounds a number to the largest integer smaller than or equal to a number.
* &#x20;**math.pi** - Returns the value of pi (\~3.14).
* &#x20;**math.huge** - Returns infinite (inf).
* &#x20;**math.min(x,y, …..)** - Returns the smallest value.
* &#x20;**math.max(x,y, …..)** - Returns the largest value.
* &#x20;**math.random(x,y)** - Returns a random value in the range of \[x,y].
