The math module provides integer math utilities. All functions operate on Int values unless noted.
from math import clamp_int, factorial, gcd
let clamped = clamp_int(150, 0, 100) # 100
let f = factorial(5) # 120
let g = gcd(12, 8) # 4
Clamping
| Function | Returns | Description |
clamp_int(x, low, high) | Int | Constrain x to the range [low, high] |
clamp_float(x, low, high) | Float | Constrain x to the range [low, high] |
from math import clamp_int, clamp_float
let a = clamp_int(-5, 0, 100) # 0
let b = clamp_int(50, 0, 100) # 50
let c = clamp_float(3.14, 0.0, 1.0) # 1.0
Sign and predicates
| Function | Returns | Description |
sign(x) | Int | Returns 1 if positive, -1 if negative, 0 if zero |
is_positive(x) | Bool | True if x > 0 |
is_negative(x) | Bool | True if x < 0 |
is_even(x) | Bool | True if x is divisible by 2 |
is_odd(x) | Bool | True if x is not divisible by 2 |
from math import sign, is_even, is_odd
print(sign(-42)) # -1
print(is_even(10)) # True
print(is_odd(7)) # True
Combinatorics
| Function | Returns | Description |
factorial(n) | Int | Product of 1 to n; returns 1 for n=0 |
gcd(a, b) | Int | Greatest common divisor of a and b |
lcm(a, b) | Int | Least common multiple of a and b |
from math import factorial, gcd, lcm
let f = factorial(6) # 720
let g = gcd(48, 18) # 6
let l = lcm(4, 6) # 12