Arithmetic Operators: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Quenty
Changed a few things. Added extra 'See also'
Adding ScriptTutorial template
 
(25 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Roblox [http://www.lua.org/pil/index.html Lua] can do mathematical calculations, much like any calculator.  In Lua, there is:
{{ScriptTutorial|easy|scripting}}
Roblox Lua can do mathematical calculations, much like any calculator.  In Lua, there are the following arithmetic operators:


* + Addition (e.g., 2 + 3 = 5)
* + Addition (e.g., 2 + 3 = 5)
Line 7: Line 8:
* - Unary Negation (e.g., -(20) = -20)
* - Unary Negation (e.g., -(20) = -20)
* ^ Exponentiation (e.g., 5 ^ 5 = 3125)
* ^ Exponentiation (e.g., 5 ^ 5 = 3125)
* % Modulus (Percent) (Returns the remainder of a division between two numbers e.g. 11 % 3 = 2)
*% Modulus (Percent) (Returns the remainder of a division between two numbers e.g. 11 % 3 = 2)




{{Example|<code>
{{code and output
print(2 + 3) --> 5
|fit=code
print(5 - 2) --> 3
|code=
print(5 * 2) --> 10
print(2 + 3)
print(8 / 2) --> 4
print(5 - 2)
print(-(20)) --> -20
print(5 * 2)
print(23 % 4) --> 3
print(8 / 2)
</code>}}
print(-(20))
print(23 % 4)
|output=
5
3
10
4
-20
3
}}




==Parentheses==
==Parentheses==
Parentheses can be used in Lua much as they are used in algebra. If you want something calculated first, put that in parentheses.
Parentheses can be used in Lua. If you want something calculated first, put it in parentheses.


{{Example|<code>
'''Example:'''
<syntaxhighlight lang="lua">
print((10 / 20) / 5) -- is equal to .5 / 5, which is .1
print((10 / 20) / 5) -- is equal to .5 / 5, which is .1
print(10 / (20 / 5)) -- is equal to 10 / 4, which is 2.5
print(10 / (20 / 5)) -- is equal to 10 / 4, which is 2.5
</code>}}
</syntaxhighlight>


Following the associative properties of multiplication and addition, it does not matter how you organize the parentheses in certain instances:
Following the associative properties of multiplication and addition, it does not matter how you organize the parentheses in certain instances.


{{Example|<code>
'''Example:'''
<syntaxhighlight lang="lua">
print((10 + 20) + 5) -- is equal to 30 + 5, which is 35
print((10 + 20) + 5) -- is equal to 30 + 5, which is 35
print(10 + (20 + 5)) -- is equal to 10 + 25, which is 35
print(10 + (20 + 5)) -- is equal to 10 + 25, which is 35
Line 36: Line 48:
print((10 * 20) * 5) -- is equal to 200 * 5, which is 1000
print((10 * 20) * 5) -- is equal to 200 * 5, which is 1000
print(10 * (20 * 5)) -- is equal to 10 * 100, which is 1000
print(10 * (20 * 5)) -- is equal to 10 * 100, which is 1000
</code>}}
</syntaxhighlight>


==Precedence==
==Precedence==
Lua follows the same order of operations as defined by the rules of mathematics.  The mnemonic ''PEMDAS'' (parentheses, exponents, multiplication, division, addition, and subtraction) apply to Lua.  The following goes from lower, to higher priority.
{{main|Precedence}}
For the mathematic operators, Lua follows the same rules as the ones usually followed in mathematics, the ones you have been taught or will be taught at school, with the modulus operator having the same priority as multiplication and division. That means mathematic operations will be executed in the order below:


      +    -
#Exponentiation (^)
      *     /     %
#Unary Negation (-)
      - (unary)
#Multiplication, division and modulus (*, / and %)
      ^
#Addition and subtraction (+ and -)
 
Just like in normal math, you can use parenthesis to change the order.
 
Note that multiplication and division have the same precedence, as well as addition and subtraction. This means that they will occur in whatever order they appear from left-to-right after exponents, '''not''' necessarily multiplication and then division.


==See Also==
==See Also==

Latest revision as of 21:29, 28 April 2023

This is an easy, scripting related tutorial.

Roblox Lua can do mathematical calculations, much like any calculator. In Lua, there are the following arithmetic operators:

  • + Addition (e.g., 2 + 3 = 5)
  • - Subtraction (e.g., 5 - 2 = 3)
  • * Multiplication (e.g., 5 * 2 = 10)
  • / Division (e.g., 8 / 2 = 4)
  • - Unary Negation (e.g., -(20) = -20)
  • ^ Exponentiation (e.g., 5 ^ 5 = 3125)
  • % Modulus (Percent) (Returns the remainder of a division between two numbers e.g. 11 % 3 = 2)


print(2 + 3)
print(5 - 2)
print(5 * 2)
print(8 / 2)
print(-(20))
print(23 % 4)

5 3 10 4 -20

3


Parentheses

Parentheses can be used in Lua. If you want something calculated first, put it in parentheses.

Example:

print((10 / 20) / 5) -- is equal to .5 / 5, which is .1
print(10 / (20 / 5)) -- is equal to 10 / 4, which is 2.5

Following the associative properties of multiplication and addition, it does not matter how you organize the parentheses in certain instances.

Example:

print((10 + 20) + 5) -- is equal to 30 + 5, which is 35
print(10 + (20 + 5)) -- is equal to 10 + 25, which is 35

print((10 * 20) * 5) -- is equal to 200 * 5, which is 1000
print(10 * (20 * 5)) -- is equal to 10 * 100, which is 1000

Precedence

Main article: Precedence

For the mathematic operators, Lua follows the same rules as the ones usually followed in mathematics, the ones you have been taught or will be taught at school, with the modulus operator having the same priority as multiplication and division. That means mathematic operations will be executed in the order below:

  1. Exponentiation (^)
  2. Unary Negation (-)
  3. Multiplication, division and modulus (*, / and %)
  4. Addition and subtraction (+ and -)

Just like in normal math, you can use parenthesis to change the order.

Note that multiplication and division have the same precedence, as well as addition and subtraction. This means that they will occur in whatever order they appear from left-to-right after exponents, not necessarily multiplication and then division.

See Also