math
Module math provides constants and mathematical functions. It is primarily
a wrapper of the Go math (opens in a new tab) package, however it also
includes a sum function for summing a list of numbers.
Risor provides equivalence between float and int types, so many of the functions in this module accept both float and int as inputs. In the documentation below, "number" is used to refer to either float or int.
Constants
PI
PI float>>> math.PI
3.141592653589793E
E float>>> math.E
2.718281828459045Functions
abs
abs(x number) numberReturns the absolute value of x.
>>> math.abs(-2)
2
>>> math.abs(3.3)
3.3atan2
atan2(y, x number) numberReturns the arc tangent value of y/x.
>>> math.atan2(1, 2)
0.4636476090008061sqrt
sqrt(x number) floatReturns the square root of x.
>>> math.sqrt(4)
2
>>> math.sqrt(2)
1.4142135623730951min
min(x, y number) floatReturns the smaller of x or y.
>>> math.min(1, 2)
1.0
>>> math.min(3, -2.5)
-2.5max
max(x, y number) floatReturns the larger of x or y.
>>> math.max(1, 2)
2.0
>>> math.max(-3, 2.5)
2.5floor
floor(x number) numberReturns the largest integer value less than or equal to x.
>>> math.floor(2.5)
2
>>> math.floor(-2.5)
-3
>>> math.floor(3)
3ceil
ceil(x number) numberReturns the smallest integer value greater than or equal to x.
>>> math.ceil(2.5)
3
>>> math.ceil(-2.5)
-2sin
sin(x number) floatReturns the sine of x.
>>> math.sin(0)
0
>>> math.sin(math.PI / 2)
1cos
cos(x number) floatReturns the cosine of x.
>>> math.cos(0)
1
>>> math.cos(math.PI / 2)
0tan
tan(x number) floatReturns the tangent of x.
>>> math.tan(0)
0
>>> math.tan(math.PI / 4)
0.9999999999999998mod
mod(x, y number) floatReturns the remainder of x divided by y.
>>> math.mod(5, 2)
1
>>> math.mod(5, 3)
2log
log(x number) floatReturns the natural logarithm of x.
>>> math.log(1)
0
>>> math.log(math.E)
1log10
log10(x number) floatReturns the base 10 logarithm of x.
>>> math.log10(1)
0
>>> math.log10(10)
1log2
log2(x number) floatReturns the base 2 logarithm of x.
>>> math.log2(1)
0
>>> math.log2(8)
3pow
pow(x, y number) floatReturns x raised to the power of y.
>>> math.pow(2, 3)
8
>>> math.pow(2, 0.5)
1.4142135623730951pow10
pow10(x number) floatReturns 10 raised to the power of x.
>>> math.pow10(0)
1
>>> math.pow10(1)
10
>>> math.pow10(2)
100inf
inf(x number) float Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
>>> math.inf()
+Infis_inf
is_inf(x number) boolReturns true if x is positive or negative infinity.
>>> math.is_inf(math.inf)
true
>>> math.is_inf(-math.inf)
true
>>> math.is_inf(0)
falseround
round(x number) floatReturns x rounded to the nearest integer.
>>> math.round(1.4)
1
>>> math.round(1.5)
2sum
sum(list) floatReturns the sum of all numbers in a list.
>>> math.sum([1, 2, 3])
6
>>> math.sum([])
0