Arithmetic Operators: Difference between revisions
>Quenty No edit summary |
>Samacado fixed some grammar and layout stuff so it is pretty and consistent. PEMDAS always mislead me in elementary and middle school. so I added a note about it. |
||
Line 1: | Line 1: | ||
Roblox [http://www.lua.org/pil/index.html Lua] can do mathematical calculations, much like any calculator. In Lua, there | Roblox [http://www.lua.org/pil/index.html Lua] can do mathematical calculations, much like any calculator. In Lua, there are: | ||
* + Addition (e.g., 2 + 3 = 5) | * + Addition (e.g., 2 + 3 = 5) | ||
Line 7: | Line 7: | ||
* - 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) | ||
Line 39: | Line 39: | ||
==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. That means | 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. That means operations will be executed in the order below. | ||
1) Parenthesis () | :1) Parenthesis ()<br /> | ||
2) Exponents (^) | :2) Exponents (^)<br /> | ||
3) Multiplication and Division (/ or *) | :3) Multiplication and Division (/ or *)<br /> | ||
4) Addition or Subtraction (- or +) | :4) Addition or Subtraction (- or +)<br /> | ||
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 parentheses and exponents, '''not''' necessarily multiplication and then division. | |||
That means: | That means: |
Revision as of 01:37, 14 January 2012
Roblox Lua can do mathematical calculations, much like any calculator. In Lua, there are:
- + 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) --> 5
print(5 - 2) --> 3
print(5 * 2) --> 10
print(8 / 2) --> 4
print(-(20)) --> -20
print(23 % 4) --> 3
Parentheses
Parentheses can be used in Lua much as they are used in algebra. If you want something calculated first, put that in parentheses.
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:
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
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. That means operations will be executed in the order below.
- 1) Parenthesis ()
- 2) Exponents (^)
- 3) Multiplication and Division (/ or *)
- 4) Addition or Subtraction (- or +)
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 parentheses and exponents, not necessarily multiplication and then division.
That means:
2(3*3) - (3+3) will result in 12.