# Math API

## Functions

 Functionmath.abs
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(
• num : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter -5, the number printed would be 5.
 Functionmath.ceil
Return num rounded to the next whole integer.
Syntax math.ceil(
• num : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter 0.2, the number printed would be 1.
 Functionmath.cos
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(
• num : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter 90, the number printed would be -1.
 Functionmath.deg
Convert the number num from radians to degrees.
Syntax math.deg(
• num : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter -1, the number printed would be -90.
 Functionmath.exp
Returns e raised to the power of num.
Syntax math.exp(
• num : number
)
Returns number
Part of Lua (source)
API math
 ExampleCompute e
Prints the approximate value of Euler's constant, ${\displaystyle e}$
Code
<nowiki>
print(math.exp(1))
</nowiki>

Output
2.718281828459

 Functionmath.floor
Return num rounded to the previous whole integer.
Syntax math.floor(
• num : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter 0.2, the number printed would be 0.
 Functionmath.mod
Return the remainder of dividing x by y. This has the same behaviour as the % operator.
Syntax math.mod(
• x : number
• y : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter 5, the value printed would be false.
 Functionmath.frexp
Return the mantissa and exponent of num, two numbers ${\displaystyle m}$ and ${\displaystyle e}$ such that ${\displaystyle {\text{num}}=2m^{e}}$
Syntax math.frexp(
• num : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter 5, the numbers printed would be 0.625 3.
 Functionmath.ldexp
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(
• mantissa : number
• exponent : number
)
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
<nowiki>
print(math.ldexp(m, e))
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter 0.625 and 3, the number print would be 5
 Functionmath.max
Return the greatest of all the numbers given.
Syntax math.max(
• numbers... : number
)
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
<nowiki>
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)
</nowiki>

Output The line the user wrote with the greatest value, or inf if they wrote no lines.
 Functionmath.min
Return the smallest of all the numbers given.
Syntax math.min(
• numbers... : number
)
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
<nowiki>
local max = math.huge
while true do
if tonumber(l) then
max = math.min(max, tonumber(l))
else
break
end
end
print(max)
</nowiki>

Output The line the user wrote with the smallest value, or inf if they wrote no lines.
 Functionmath.modf
Return the integer and fractional parts of number.
Syntax math.modf(
• number : number
)
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
<nowiki>
</nowiki>

Output Depends. If the user wrote 5.2, the numbers printed will be 5 0.2
 Functionmath.pow
Return x raised to the yth power. This is equivalent to the operator ^.
Syntax math.pow(
• x : number
• y : number
)
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
<nowiki>
</nowiki>

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.
• num : number
)
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
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter -1, the number printed would be -90.
 Functionmath.randomseed
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(
• seed : number
)
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
<nowiki>
math.randomseed(1234)
print("First: " .. math.random())

math.randomseed(1234)
print("Second: " .. math.random())
</nowiki>

Output First: 0.12414929654836
Second: 0.12414929654836
 Functionmath.sin
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(
• num : number
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the sine of user input
Code
<nowiki>
</nowiki>

Output Depends on what the user wrote. For instance, if they were to enter 90, the number printed would be 1.
 Functionmath.sqrt
Returns the square root of num.
Syntax math.sqrt(
• num : number
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the square root of 2.
Prints an approximation of ${\displaystyle {\sqrt {2}}}$.
Code
<nowiki>
print(math.sqrt(2))
</nowiki>

Output 1.4142135623731
 Functionmath.tan
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(
• num : number
)
Returns number
Part of Lua (source)
API math
 ExamplePrint the tangent of user input
Code
<nowiki>
</nowiki>

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

## Constants

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

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

Part of CC:Tweaked
gollark: Oh, this is part of an implementation of a blockchain for some reason? Interesting.
gollark: Oh, Jupyter, right. Still weird.
gollark: Is that Colab or something? Why are they using that for sharing documents?
gollark: I think you have lots of emotional attachment to cryptocurrency because you made money off it and something something tribalism.
gollark: Because modern silicon processes are *really* energy-intensive.