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>
print(math.abs(tonumber(read())))
    </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>
print(math.ceil(tonumber(read())))
    </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>
print(math.cos(math.rad(tonumber(read()))))
    </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>
print(math.deg(math.asin(tonumber(read()))))
    </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,
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>
print(math.floor(tonumber(read())))
    </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>
print(math.mod(tonumber(read()), 2) == 0)
    </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 and such that
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>
print(math.frexp(tonumber(read())))
    </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 .
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>
local m = tonumber(read())
local e = tonumber(read())
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
  local l = read()
  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
  local l = read()
  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>
print(math.modf(tonumber(read())))
    </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>
print(math.pow(2, tonumber(read())))
    </nowiki>
Output Depends on what the user wrote. For instance, if they were to enter 5, the value printed would be 32.
Functionmath.rad
Convert the number num from degrees to radians.
Syntax math.rad(
  • 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>
print(math.sin(math.rad(tonumber(read())))
    </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
Read a line from the user, convert it to radians using math.rad, then print its sine.
Code
<nowiki>
print(math.sin(math.rad(tonumber(read())))
    </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 .
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
Read a line from the user, convert it to radians using math.rad, then print its tangent.
Code
<nowiki>
print(math.tan(math.rad(tonumber(read())))
    </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.
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.