Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.

Math

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

FunctionReturnsDescription
clamp_int(x, low, high)IntConstrain x to the range [low, high]
clamp_float(x, low, high)FloatConstrain 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

FunctionReturnsDescription
sign(x)IntReturns 1 if positive, -1 if negative, 0 if zero
is_positive(x)BoolTrue if x > 0
is_negative(x)BoolTrue if x < 0
is_even(x)BoolTrue if x is divisible by 2
is_odd(x)BoolTrue 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

FunctionReturnsDescription
factorial(n)IntProduct of 1 to n; returns 1 for n=0
gcd(a, b)IntGreatest common divisor of a and b
lcm(a, b)IntLeast 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