Skip to content
Docs
Modules
math

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

Constant
PI float
Example
>>> math.PI
3.141592653589793

E

Constant
E float
Example
>>> math.E
2.718281828459045

Functions

abs

Function signature
abs(x number) number

Returns the absolute value of x.

Example
>>> math.abs(-2)
2
>>> math.abs(3.3)
3.3

sqrt

Function signature
sqrt(x number) float

Returns the square root of x.

Example
>>> math.sqrt(4)
2
>>> math.sqrt(2)
1.4142135623730951

min

Function signature
min(x, y number) float

Returns the smaller of x or y.

Example
>>> math.min(1, 2)
1.0
>>> math.min(3, -2.5)
-2.5

max

Function signature
max(x, y number) float

Returns the larger of x or y.

Example
>>> math.max(1, 2)
2.0
>>> math.max(-3, 2.5)
2.5

floor

Function signature
floor(x number) number

Returns the largest integer value less than or equal to x.

Example
>>> math.floor(2.5)
2
>>> math.floor(-2.5)
-3
>>> math.floor(3)
3

ceil

Function signature
ceil(x number) number

Returns the smallest integer value greater than or equal to x.

Example
>>> math.ceil(2.5)
3
>>> math.ceil(-2.5)
-2

sin

Function signature
sin(x number) float

Returns the sine of x.

Example
>>> math.sin(0)
0
>>> math.sin(math.PI / 2)
1

cos

Function signature
cos(x number) float

Returns the cosine of x.

Example
>>> math.cos(0)
1
>>> math.cos(math.PI / 2)
0

tan

Function signature
tan(x number) float

Returns the tangent of x.

Example
>>> math.tan(0)
0
>>> math.tan(math.PI / 4)
0.9999999999999998

mod

Function signature
mod(x, y number) float

Returns the remainder of x divided by y.

Example
>>> math.mod(5, 2)
1
>>> math.mod(5, 3)
2

log

Function signature
log(x number) float

Returns the natural logarithm of x.

Example
>>> math.log(1)
0
>>> math.log(math.E)
1

log10

Function signature
log10(x number) float

Returns the base 10 logarithm of x.

Example
>>> math.log10(1)
0
>>> math.log10(10)
1

log2

Function signature
log2(x number) float

Returns the base 2 logarithm of x.

Example
>>> math.log2(1)
0
>>> math.log2(8)
3

pow

Function signature
pow(x, y number) float

Returns x raised to the power of y.

Example
>>> math.pow(2, 3)
8
>>> math.pow(2, 0.5)
1.4142135623730951

pow10

Function signature
pow10(x number) float

Returns 10 raised to the power of x.

Example
>>> math.pow10(0)
1
>>> math.pow10(1)
10
>>> math.pow10(2)
100

is_inf

Function signature
is_inf(x number) bool

Returns true if x is positive or negative infinity.

Example
>>> math.is_inf(math.inf)
true
>>> math.is_inf(-math.inf)
true
>>> math.is_inf(0)
false

round

Function signature
round(x number) float

Returns x rounded to the nearest integer.

Example
>>> math.round(1.4)
1
>>> math.round(1.5)
2

sum

Function signature
sum(list) float

Returns the sum of all numbers in a list.

Example
>>> math.sum([1, 2, 3])
6
>>> math.sum([])
0