math.abs

From ComputerCraft
Jump to: navigation, search

math.abs
Function
Syntax
math.abs()
Returns number
API math
Source Lua (source)

Return the absolute value of num (i.e. the number's distance from zero). This means that negative numbers will be converted to a positive number (e.g. -5 into 5), and positive numbers remain unaffected. The return value will always be a positive number.

ExampleExamplePrint the absolute value of user input
Read a line from the user, then print the absolute value of the number they entered.
Code
print(math.abs(tonumber(read())))
Output Depends on what the user wrote. For instance, if they were to enter -5, the number printed would be 5.