Skip to content

math module

The math module provides access to a set of commonly used mathematical functions and constants.

Constants:

  • math.E (e: 2.718281828459045...)
  • math.PHI (φ: 1.618033988749894...)
  • math.PI (π: 3.141592653589793...)
  • math.TAU (τ: 6.283185307179586...)

Function                              Use
abs(n) Returns the absolute value of n.
ceil(x) Returns the least integer ≥ x.
factorial(n) Returns n factorial.
floor(x) Returns the greatest integer ≤ x.
gcd(a, b) Returns the greatest common divisor of a and b.
If either argument is zero, the absolute value of the other argument will be returned.
is_int(x) Returns 1 if x is an integer, 0 otherwise. Equivalent to x :: x$.
is_prime(n) Returns 1 if n is prime, 0 otherwise.
lcm(a, b) Returns the least common multiple of a and b.
If any of the arguments is zero,
then the returned value is 0.
max(array[, values...])  Returns the largest value in array if only one argument is passed,
otherwise returns the largest value in [array] + values.
min(array[, values...])  Returns the smallest value in array if only one argument is passed,
otherwise returns the smallest value in [array] + values.
product(array) Multiplies the items of array from left to right and returns the total.
The array's items must be numbers.
round(x[, ndigits]) Returns x rounded to ndigits precision after the decimal point.
Works exactly like Python's round().
shl(a, b) Returns a shifted to the left by b bits.
shr(a, b) Returns a shifted to the right by b bits.
sqrt(x) Returns the square root of the nonnegative number n.
sum(array[, start]) Sums start and the items of array from left to right and returns the total.
start defaults to 0.
to_bin(n) Returns the binary representation of n as a string.
to_oct(n) Returns the octal representation of n as a string.
to_hex(n) Returns the hexadecimal representation of n as a string.