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.141592653589793
E
E float
>>> math.E
2.718281828459045
Functions
abs
abs(x number) number
Returns the absolute value of x.
>>> math.abs(-2)
2
>>> math.abs(3.3)
3.3
sqrt
sqrt(x number) float
Returns the square root of x.
>>> math.sqrt(4)
2
>>> math.sqrt(2)
1.4142135623730951
min
min(x, y number) float
Returns the smaller of x or y.
>>> math.min(1, 2)
1.0
>>> math.min(3, -2.5)
-2.5
max
max(x, y number) float
Returns the larger of x or y.
>>> math.max(1, 2)
2.0
>>> math.max(-3, 2.5)
2.5
floor
floor(x number) number
Returns the largest integer value less than or equal to x.
>>> math.floor(2.5)
2
>>> math.floor(-2.5)
-3
>>> math.floor(3)
3
ceil
ceil(x number) number
Returns the smallest integer value greater than or equal to x.
>>> math.ceil(2.5)
3
>>> math.ceil(-2.5)
-2
sin
sin(x number) float
Returns the sine of x.
>>> math.sin(0)
0
>>> math.sin(math.PI / 2)
1
cos
cos(x number) float
Returns the cosine of x.
>>> math.cos(0)
1
>>> math.cos(math.PI / 2)
0
tan
tan(x number) float
Returns the tangent of x.
>>> math.tan(0)
0
>>> math.tan(math.PI / 4)
0.9999999999999998
mod
mod(x, y number) float
Returns the remainder of x divided by y.
>>> math.mod(5, 2)
1
>>> math.mod(5, 3)
2
log
log(x number) float
Returns the natural logarithm of x.
>>> math.log(1)
0
>>> math.log(math.E)
1
log10
log10(x number) float
Returns the base 10 logarithm of x.
>>> math.log10(1)
0
>>> math.log10(10)
1
log2
log2(x number) float
Returns the base 2 logarithm of x.
>>> math.log2(1)
0
>>> math.log2(8)
3
pow
pow(x, y number) float
Returns x raised to the power of y.
>>> math.pow(2, 3)
8
>>> math.pow(2, 0.5)
1.4142135623730951
pow10
pow10(x number) float
Returns 10 raised to the power of x.
>>> math.pow10(0)
1
>>> math.pow10(1)
10
>>> math.pow10(2)
100
is_inf
is_inf(x number) bool
Returns true if x is positive or negative infinity.
>>> math.is_inf(math.inf)
true
>>> math.is_inf(-math.inf)
true
>>> math.is_inf(0)
false
round
round(x number) float
Returns x rounded to the nearest integer.
>>> math.round(1.4)
1
>>> math.round(1.5)
2
sum
sum(list) float
Returns the sum of all numbers in a list.
>>> math.sum([1, 2, 3])
6
>>> math.sum([])
0