# Math API

## Functions

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.
Syntax math.abs(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint 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.

Return num rounded to the next whole integer.
Syntax math.ceil(
)
Returns number
Part of Lua (source)
API math
 ExampleRound the value of user input
Read a line from the user, then print the ceiling of the number they entered.
Code
print(math.ceil(tonumber(read())))

Output Depends on what the user wrote. For instance, if they were to enter 0.2, the number printed would be 1.

Return the cosine of num.  Note: The argument must be given in radians. If it is in degrees, use the math.rad function to convert it.
Syntax math.cos(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the cosine of user input
Read a line from the user, convert it to radians, then print its cosine.
Code
print(math.cos(math.rad(tonumber(read()))))

Output Depends on what the user wrote. For instance, if they were to enter 90, the number printed would be -1.

Convert the number num from radians to degrees.
Syntax math.deg(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the inverse sine of user input in degrees
Read a line from the user, compute its arcsin, then convert it to degrees.
Code
print(math.deg(math.asin(tonumber(read()))))

Output Depends on what the user wrote. For instance, if they were to enter -1, the number printed would be -90.

Returns e raised to the power of num.
Syntax math.exp(
)
Returns number
Part of Lua (source)
API math
 ExampleCompute e
Prints the approximate value of Euler's constant, ${\displaystyle e}$
Code
print(math.exp(1))

Output
2.718281828459


Return num rounded to the previous whole integer.
Syntax math.floor(
)
Returns number
Part of Lua (source)
API math
 ExampleRound the value of user input
Read a line from the user, then print the floor of the number they entered.
Code
print(math.floor(tonumber(read())))

Output Depends on what the user wrote. For instance, if they were to enter 0.2, the number printed would be 0.

Return the remainder of dividing x by y. This has the same behaviour as the % operator.
Syntax math.mod(
)
Returns number
Part of Lua (source)
API math
 ExampleTest if user input is even
Read a line from the user, then print if it is evenly divisible by 2.
Code
print(math.mod(tonumber(read()), 2) == 0)

Output Depends on what the user wrote. For instance, if they were to enter 5, the value printed would be false.

Return the mantissa and exponent of num, two numbers ${\displaystyle m}$ and ${\displaystyle e}$ such that ${\displaystyle {\text{num}}=2m^{e}}$
Syntax math.frexp(
)
Returns number mantissa, number exponent
Part of Lua (source)
API math
 ExampleDecompose user input input
Read a line from the user, then print the mantissa and exponent of the number they entered.
Code
print(math.frexp(tonumber(read())))

Output Depends on what the user wrote. For instance, if they were to enter 5, the numbers printed would be 0.625 3.

Compute a floating point number from the given mantissa and exponent. This is the inverse operation of math.frexp, and computes ${\displaystyle 2{\text{mantissa}}^{\text{exponent}}}$.
Syntax math.ldexp(
)
Returns number
Part of Lua (source)
API math
 ExampleTurn user input into a number
Read a two lines from the user, and compute the floating point number represented by the given mantissa and exponent.
Code
local m = tonumber(read())
print(math.ldexp(m, e))

Output Depends on what the user wrote. For instance, if they were to enter 0.625 and 3, the number print would be 5

Return the greatest of all the numbers given.
Syntax math.max(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the maximum value of user input
Read as many lines as the user inputs, and return the greatest value that they wrote.
Code
local max = -(1/0) -- negative infinity
while true do
if tonumber(l) then
max = math.max(max, tonumber(l))
else
break
end
end
print(max)

Output The line the user wrote with the greatest value, or inf if they wrote no lines.

Return the smallest of all the numbers given.
Syntax math.min(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the minimum value of user input
Read as many lines as the user inputs, and return the smallest value that they wrote.
Code
local max = math.huge
while true do
if tonumber(l) then
max = math.min(max, tonumber(l))
else
break
end
end
print(max)

Output The line the user wrote with the smallest value, or inf if they wrote no lines.

Return the integer and fractional parts of number.
Syntax math.modf(
)
Returns number integral, number fractional
Part of Lua (source)
API math
 ExamplePrint the integral and fractional parts of user input
Read a single number from the user, then print its integral and fractional parts.
Code
print(math.modf(tonumber(read())))

Output Depends. If the user wrote 5.2, the numbers printed will be 5 0.2

Return x raised to the yth power. This is equivalent to the operator ^.
Syntax math.pow(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the n-th power of two
Read a line from the user, then print two raised to that number.
Code
print(math.pow(2, tonumber(read())))

Output Depends on what the user wrote. For instance, if they were to enter 5, the value printed would be 32.

Convert the number num from degrees to radians.
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the sine of user input
Read a line from the user, convert it to radians, then print its math.sin.
Code
print(math.sin(math.rad(tonumber(read())))

Output Depends on what the user wrote. For instance, if they were to enter -1, the number printed would be -90.

Sets the seed for the random number generator used by math.random. The same seeds produce will always produce the same random numbers.
Syntax math.randomseed(
)
Returns nil
Part of Lua (source)
API math
 ExampleGenerate two sequences with the same seed
Print two sequences of randomly generated numbers using the same seed
Code
math.randomseed(1234)
print("First: " .. math.random())

math.randomseed(1234)
print("Second: " .. math.random())

Output First: 0.12414929654836
Second: 0.12414929654836

Returns the sine of num.  Note: The argument must be given in radians. If it is in degrees, use the math.rad function to convert it.
Syntax math.sin(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the sine of user input
Code
print(math.sin(math.rad(tonumber(read())))

Output Depends on what the user wrote. For instance, if they were to enter 90, the number printed would be 1.

Returns the square root of num.
Syntax math.sqrt(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the square root of 2.
Prints an approximation of ${\displaystyle {\sqrt {2}}}$.
Code
print(math.sqrt(2))

Output 1.4142135623731

Returns the tangent of num.  Note: The argument must be given in radians. If it is in degrees, use the math.rad function to convert it.
Syntax math.tan(
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the tangent of user input
Code
print(math.tan(math.rad(tonumber(read())))

Output Depends. If the user wrote 45, the output would be 1.

## Constants

Floating point positive infinity, a number greater than any other.
Type number
Value
inf

Part of CC:Tweaked
An approximation of the mathematical constant π.
Type number
Value
3.1415926535898

Part of CC:Tweaked