Math
This page documents the math library in Pluto, which is built on top of Lua 5.5's.
Added
math.isnan
Checks if a number is NaN.
Parameters
- The number to check.
plutolocal x = 0 / 0print(x ~= x) -- Lua way: Prove the variable is NaN because it is not equal to itself. Works, but unintuitive.print(math.isnan(x))
math.clz
Returns the number of leading zero bits in an integer's binary representation.
Parameters
- The integer to inspect.
plutolocal bitwidth = math.popcnt(~0)print(math.clz(1 << (bitwidth - 1))) --> 0print(math.clz(0) == bitwidth) --> true
math.ctz
Returns the number of trailing zero bits in an integer's binary representation.
Parameters
- The integer to inspect.
plutolocal bitwidth = math.popcnt(~0)print(math.ctz(0b101000)) --> 3print(math.ctz(0) == bitwidth) --> true
math.popcnt
Returns the number of set bits in an integer's binary representation.
Parameters
- The integer to inspect.
plutoprint(math.popcnt(0)) --> 0print(math.popcnt(0b10101)) --> 3
math.round
Rounds a number to the nearest integer.
Parameters
- The number to round.
plutoprint(math.round(2.4)) --> 2print(math.round(2.5)) --> 3
math.trunc
Truncates the fractional part of a number, returning the integer portion toward zero.
Parameters
- The number to truncate.
plutoprint(math.trunc(10.5)) --> 10print(math.trunc(-10.5)) --> -10
math.cbrt
Returns the cube root of a number.
Parameters
- The number to take the cube root of.
plutoprint(math.cbrt(27)) --> 3print(math.cbrt(-27)) --> -3
math.hypot
Returns the square root of the sum of squares of its arguments.
Parameters
- Two numbers representing orthogonal components.
plutoprint(math.hypot(3, 4)) --> 5print(math.hypot(1e155, 1e155)) --> 1.41421e155
math.atan2
An alias for the math.atan function.
math.inf
An alias for the math.huge constant.
Unchanged
math.abs
Returns the absolute value of a number.
Parameters
- The number to get the absolute value of.
plutoprint(math.abs(-5)) --> 5print(math.abs(2.5)) --> 2.5
math.acos
Returns the arc cosine of a number, in radians.
Parameters
- The cosine value, in the range [-1, 1].
plutoprint(math.acos(0)) --> 1.5707963267948966print(math.acos(1)) --> 0.0
math.asin
Returns the arc sine of a number, in radians.
Parameters
- The sine value, in the range [-1, 1].
plutoprint(math.asin(0)) --> 0.0print(math.asin(1)) --> 1.5707963267948966
math.atan
Returns the arc tangent of y / x, in radians. The signs of both arguments are used to determine the quadrant of the result, and the case of x being zero is handled correctly.
Parameters
- The
ycoordinate. - The
xcoordinate. Defaults to1, somath.atan(y)returns the arc tangent ofy.
plutoprint(math.atan(0, 1)) --> 0.0print(math.atan(1)) --> 0.78539816339744828assert(math.atan(1) == math.pi / 4)
math.ceil
Returns the smallest integral value greater than or equal to a number.
Parameters
- The number to round up.
plutoprint(math.ceil(4.2)) --> 5print(math.ceil(-4.2)) --> -4
math.cos
Returns the cosine of an angle.
Parameters
- The angle, in radians.
plutoprint(math.cos(0)) --> 1.0print(math.cos(1)) --> 0.54030230586813977
math.deg
Converts an angle from radians to degrees.
Parameters
- The angle, in radians.
plutoprint(math.deg(math.pi)) --> 180.0
math.exp
Returns the value ex (where e is the base of natural logarithms).
Parameters
- The exponent (
x).
plutoprint(math.exp(0)) --> 1.0print(math.exp(1)) --> 2.7182818284590451
math.floor
Returns the largest integral value less than or equal to a number.
Parameters
- The number to round down.
plutoprint(math.floor(4.8)) --> 4print(math.floor(-4.2)) --> -5
math.fmod
Returns the result of a truncated modulo operation.
Parameters
- The dividend.
- The divisor.
plutolocal a = -7local b = 3print(a % b) --> 2print(math.fmod(a, b)) --> -1
math.frexp
Returns two numbers m and e such that x = m * 2^e, where e is an integer. When x is zero, NaN, +inf, or -inf, m is equal to x; otherwise, the absolute value of m is in the range [0.5, 1).
Parameters
- The number to decompose (
x).
plutolocal m, e = math.frexp(8)print(m) --> 0.5print(e) --> 4print(m * 2^e) --> 8.0
math.ldexp
Returns m * 2^e, where e is an integer.
Parameters
- The mantissa.
- The integer exponent.
plutoprint(math.ldexp(0.5, 4)) --> 8.0
math.huge
A constant float value that is greater than any other numeric value.
plutoprint(math.huge > 1e308) --> trueprint(-math.huge) --> -inf
math.log
Returns the logarithm of a number in the given base.
Parameters
- The number to take the logarithm of.
- The base. Defaults to
e, so the function returns the natural logarithm ofx.
plutoprint(math.log(1)) --> 0.0print(math.log(8, 2)) --> 3.0print(math.log(100, 10)) --> 2.0
math.max
Returns the argument with the maximum value, according to the < operator.
Parameters
- Two or more numbers to compare.
plutoprint(math.max(3, 7, 5)) --> 7
math.maxinteger
An integer with the maximum value for an integer.
plutoprint(math.maxinteger) --> 9223372036854775807
math.min
Returns the argument with the minimum value, according to the < operator.
plutoprint(math.min(3, 7, 5)) --> 3
math.mininteger
An integer with the minimum value for an integer.
plutoprint(math.mininteger) --> -9223372036854775808
math.modf
Splits a number into its integral and fractional parts.
Parameters
- The number to split.
Returns
- The integral part.
- The fractional part, always a float.
plutoprint(string.format("%d\t%.1f", math.modf(3.7))) --> 3 0.7print(string.format("%d\t%.1f", math.modf(-3.7))) --> -3 -0.7
math.pi
The value of π.
plutoprint(math.pi) --> 3.1415926535898
math.rad
Converts an angle from degrees to radians.
Parameters
- The angle, in degrees.
plutoprint(math.rad(180)) --> 3.1415926535898
math.random
Returns a pseudo-random number.
Parameters
- With no arguments, returns a float with uniform distribution in the range [0, 1).
- With two integers
mandn, returns an integer with uniform distribution in the range [m, n]. - With one positive integer
n, equivalent tomath.random(1, n). - With
0, returns an integer with all bits (pseudo)random.
plutolocal roll = math.random(1, 6)print(roll >= 1 and roll <= 6) --> true
Cryptographic code should use Pluto's crypto.random for a DRBG backed by a hardware entropy source.
math.randomseed
Reinitializes the pseudo-random generator with a seed. Equal seeds produce equal sequences of numbers.
Parameters
- When no arguments, generates a seed with a weak attempt for randomness.
- When 1 or 2 arguments, the integer parameters
xandyare joined into a seed. The default foryis zero.
Returns
The two seed components that were effectively used, so that setting them again repeats the sequence.
plutomath.randomseed(42)print(math.random(100)) --> 50print(math.randomseed(42)) --> 42 0print(math.random(100)) --> 50
math.sin
Returns the sine of an angle.
Parameters
- The angle, in radians.
plutoprint(math.sin(0)) --> 0.0print(math.sin(math.pi / 2)) --> 1.0
math.sqrt
Returns the square root of a number; equivalent to x^0.5.
Parameters
- The number to take the square root of.
plutoprint(math.sqrt(16)) --> 4.0
math.tan
Returns the tangent of an angle.
Parameters
- The angle, in radians.
plutoprint(math.tan(0)) --> 0.0
math.tointeger
Converts a value to an integer if it is convertible, otherwise returns nil.
Parameters
- The value to convert.
plutoprint(math.tointeger(3.0)) --> 3print(math.tointeger(3.5)) --> nil
math.type
Returns "integer" if the argument is an integer, "float" if it is a float, or nil if it is not a number.
Parameters
- The value to inspect.
plutoprint(math.type(1)) --> integerprint(math.type(1.0)) --> floatprint(math.type("1")) --> nil
math.ult
Returns the result of a < b with both integers interpreted as unsigned values.
Parameters
- The first integer (
a). - The second integer (
b).
plutoprint(math.ult(1, -1)) --> true