Math API

From ComputerCraft
Jump to: navigation, search

Functions

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


FunctionFunctionmath.ceil
Return num rounded to the next whole integer.
Syntax math.ceil()
Returns number
Part of Lua (source)
API math
ExampleExampleRound 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.


FunctionFunctionmath.cos
Return the cosine of num. Note 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
ExampleExamplePrint 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.


FunctionFunctionmath.deg
Convert the number num from radians to degrees.
Syntax math.deg()
Returns number
Part of Lua (source)
API math
ExampleExamplePrint 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.


FunctionFunctionmath.exp
Returns e raised to the power of num.
Syntax math.exp()
Returns number
Part of Lua (source)
API math
ExampleExampleCompute e
Prints the approximate value of Euler's constant,
Code
print(math.exp(1))
Output
2.718281828459


FunctionFunctionmath.floor
Return num rounded to the previous whole integer.
Syntax math.floor()
Returns number
Part of Lua (source)
API math
ExampleExampleRound 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.


FunctionFunctionmath.mod
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
ExampleExampleTest 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.


FunctionFunctionmath.frexp
Return the mantissa and exponent of num, two numbers and such that
Syntax math.frexp()
Returns number mantissa, number exponent
Part of Lua (source)
API math
ExampleExampleDecompose 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.


FunctionFunctionmath.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()
Returns number
Part of Lua (source)
API math
ExampleExampleTurn 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())
local e = 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


FunctionFunctionmath.max
Return the greatest of all the numbers given.
Syntax math.max()
Returns number
Part of Lua (source)
API math
ExampleExamplePrint 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
  local l = read()
  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.


FunctionFunctionmath.min
Return the smallest of all the numbers given.
Syntax math.min()
Returns number
Part of Lua (source)
API math
ExampleExamplePrint 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
  local l = read()
  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.


FunctionFunctionmath.modf
Return the integer and fractional parts of number.
Syntax math.modf()
Returns number integral, number fractional
Part of Lua (source)
API math
ExampleExamplePrint 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


FunctionFunctionmath.pow
Return x raised to the yth power. This is equivalent to the operator ^.
Syntax math.pow()
Returns number
Part of Lua (source)
API math
ExampleExamplePrint 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.


FunctionFunctionmath.rad
Convert the number num from degrees to radians.
Syntax math.rad()
Returns number
Part of Lua (source)
API math
ExampleExamplePrint 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.


FunctionFunctionmath.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()
Returns nil
Part of Lua (source)
API math
ExampleExampleGenerate 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


FunctionFunctionmath.sin
Returns the sine of num. Note 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
ExampleExamplePrint the sine of user input
Read a line from the user, convert it to radians using math.rad, then print its sine.
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.


FunctionFunctionmath.sqrt
Returns the square root of num.
Syntax math.sqrt()
Returns number
Part of Lua (source)
API math
ExampleExamplePrint the square root of 2.
Prints an approximation of .
Code
print(math.sqrt(2))
Output 1.4142135623731


FunctionFunctionmath.tan
Returns the tangent of num. Note 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
ExampleExamplePrint the tangent of user input
Read a line from the user, convert it to radians using math.rad, then print its tangent.
Code
print(math.tan(math.rad(tonumber(read())))
Output Depends. If the user wrote 45, the output would be 1.

Constants

ConstantConstantmath.huge
Floating point positive infinity, a number greater than any other.
Type number
Value
inf
Part of CC:Tweaked
ConstantConstantmath.pi
An approximation of the mathematical constant π.
Type number
Value
3.1415926535898
Part of CC:Tweaked