Math: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>Outofspace mNo edit summary |
>Camoy updated :D |
||
Line 1: | Line 1: | ||
# | ==Math== | ||
==Arithmetic Operators== | |||
Roblox [http://www.lua.org/pil/index.html Lua] can do mathematical calculations, much like a calculator. In Lua, there is: | |||
* + 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) | |||
* ^ Exponents (e.g., 5 ^ 5 = 3125) | |||
{{Example|<pre> | |||
print(2 + 3) --> 5 | |||
print(5 - 2) --> 3 | |||
print(5 * 2) --> 10 | |||
print(8 / 2) --> 4 | |||
print(-(20)) --> -20 | |||
</pre>}} | |||
''See Also: [Operator#Mathematic Mathematic Operators]'' | |||
===Parentheses=== | |||
Parentheses can be used in Lua much as they are used in algebra. If you want something calculated first, put that in parentheses. | |||
{{Example|<pre> | |||
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 | |||
</pre>}} | |||
Following the associative properties of multiplication and addition, it does not matter how you organize the parentheses in certain instances: | |||
{{Example|<pre> | |||
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 | |||
</pre>}} | |||
===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. | |||
+ - | |||
* / | |||
- (unary) | |||
^ | |||
''See Also: [http://www.lua.org/manual/5.1/manual.html#2.5.6 Complete Precedence Table]'' | |||
===See Also=== | |||
[http://www.lua.org/pil/3.1.html Arithmetic Operators] | |||
[[Category:Scripting Tutorials]] |
Revision as of 21:47, 23 January 2011
Math
Arithmetic Operators
Roblox Lua can do mathematical calculations, much like a calculator. In Lua, there is:
- + 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)
- ^ Exponents (e.g., 5 ^ 5 = 3125)
Example
print(2 + 3) --> 5 print(5 - 2) --> 3 print(5 * 2) --> 10 print(8 / 2) --> 4 print(-(20)) --> -20
See Also: [Operator#Mathematic Mathematic Operators]
Parentheses
Parentheses can be used in Lua much as they are used in algebra. If you want something calculated first, put that 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
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.
+ - * / - (unary) ^
See Also: Complete Precedence Table