veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

math

lib/math.vk

lib/math.vk Pure-Veko mathematical constants (compile-time folded), checked/saturating integer math, 64-bit bit operations, and double-precision IEEE-754 transcendentals. Zero capabilities required.

pi fn
signature
pi = fn [] f64

Circle constant pi (folded at compile time).

tau fn
signature
tau = fn [] f64

Full turn tau = 2 * pi (folded at compile time).

e fn
signature
e = fn [] f64

Euler's number e (folded at compile time).

ln2 fn
signature
ln2 = fn [] f64

Natural logarithm of 2 (folded at compile time).

signature
sqrt2 = fn [] f64

Square root of 2 (folded at compile time).

signature
i64-max = fn [] i64

Maximum signed 64-bit integer (2^63 - 1, folded at compile time).

signature
i64-min = fn [] i64

Minimum signed 64-bit integer (-2^63).

signature
abs-i64 = fn [x i64] i64

Absolute value of x (returns x for i64-min).

signature
min-i64 = fn [a i64, b i64] i64

Smaller of two signed 64-bit integers.

signature
max-i64 = fn [a i64, b i64] i64

Larger of two signed 64-bit integers.

signature
clamp-i64 = fn [x i64, lo i64, hi i64] i64

Clamps x to the inclusive range [lo, hi].

Executable Example
veko
use [m = math]
if (m/clamp-i64 15 0 10) != 10 or (m/clamp-i64 -3 0 10) != 0 [ err 'clamp-i64' ] else [ .ok ]
signature
add-checked = fn [a i64, b i64] i64

Checked 64-bit signed addition; returns err on overflow.

Executable Example
veko
use [m = math]
ok1 = (m/add-checked 10 20) == 30
ov = m/add-checked/or m/i64-max 1 [ -1 ]
if (not ok1) or ov != -1 [ err 'add-checked' ] else [ .ok ]
signature
sub-checked = fn [a i64, b i64] i64

Checked 64-bit signed subtraction; returns err on overflow.

signature
mul-checked = fn [a i64, b i64] i64

Checked 64-bit signed multiplication; returns err on overflow.

signature
add-sat = fn [a i64, b i64] i64

Saturating 64-bit signed addition (clamps at i64-min / i64-max).

signature
sub-sat = fn [a i64, b i64] i64

Saturating 64-bit signed subtraction (clamps at i64-min / i64-max).

gcd fn
signature
gcd = fn [a i64, b i64] i64

Greatest common divisor of a and b (non-negative).

Executable Example
veko
use [m = math]
if (m/gcd 48 18) != 6 or (m/lcm 12 18) != 36 [ err 'gcd/lcm' ] else [ .ok ]
lcm fn
signature
lcm = fn [a i64, b i64] i64

Least common multiple of a and b (0 if either argument is 0).

signature
pow-i64 = fn [base i64, exp i64] i64

Integer exponentiation base ^ exp for exp >= 0.

signature
isqrt = fn [n i64] i64

Integer floor square root of n >= 0.

signature
popcount = fn [x i64] i64

Counts the number of 1-bits in the 64-bit representation of x.

Executable Example
veko
use [m = math]
if (m/popcount 0xF0F0) != 8 or (m/clz 1) != 63 or (m/ctz 8) != 3 [ err 'bitops' ] else [ .ok ]
clz fn
signature
clz = fn [x i64] i64

Counts leading zero bits in the 64-bit unsigned representation of x (64 for x == 0).

ctz fn
signature
ctz = fn [x i64] i64

Counts trailing zero bits in the 64-bit representation of x (64 for x == 0).

signature
rotl64 = fn [x i64, r i64] i64

Rotates a 64-bit word left by r bits (mod 64).

signature
rotr64 = fn [x i64, r i64] i64

Rotates a 64-bit word right by r bits (mod 64).

signature
rotl32 = fn [x i64, r i64] i64

Rotates a 32-bit unsigned word left by r bits (mod 32).

signature
rotr32 = fn [x i64, r i64] i64

Rotates a 32-bit unsigned word right by r bits (mod 32).

signature
abs-f64 = fn [x f64] f64

Absolute value of x.

signature
min-f64 = fn [a f64, b f64] f64

Smaller of two f64 values.

signature
max-f64 = fn [a f64, b f64] f64

Larger of two f64 values.

signature
clamp-f64 = fn [x f64, lo f64, hi f64] f64

Clamps x to [lo, hi].

signature
isnan? = fn [x f64] bool

Returns true if x is IEEE-754 NaN.

signature
isinf? = fn [x f64] bool

Returns true if x is positive or negative infinity.

signature
trunc = fn [x f64] f64

Truncates x toward zero.

signature
floor = fn [x f64] f64

Largest integer <= x as f64.

Executable Example
veko
use [m = math]
if (m/floor 2.9) != 2.0 or (m/floor -2.1) != -3.0 or (m/ceil 2.1) != 3.0 [ err 'floor/ceil' ] else [ .ok ]
ceil fn
signature
ceil = fn [x f64] f64

Smallest integer >= x as f64.

signature
round = fn [x f64] f64

Rounds x to the nearest integer (halfway away from zero).

sqrt fn
signature
sqrt = fn [x f64] f64

Double-precision square root lowered to hardware f64/sqrt (sqrtsd / f64.sqrt / fsqrt).

Executable Example
veko
use [m = math]
d = m/abs-f64 ((m/sqrt 2.0) - m/sqrt2)
if d > 0.00000000000001 [ err 'sqrt mismatch' ] else [ .ok ]
exp fn
signature
exp = fn [x f64] f64

Exponential function e^x.

Executable Example
veko
use [m = math]
d = m/abs-f64 ((m/exp 1.0) - m/e)
if d > 0.0000000000001 [ err 'exp mismatch' ] else [ .ok ]
ln fn
signature
ln = fn [x f64] f64

Natural logarithm ln(x) for x > 0.

Executable Example
veko
use [m = math]
d = m/abs-f64 ((m/ln m/e) - 1.0)
if d > 0.0000000000001 [ err 'ln mismatch' ] else [ .ok ]
log2 fn
signature
log2 = fn [x f64] f64

Base-2 logarithm log2(x).

signature
log10 = fn [x f64] f64

Base-10 logarithm log10(x).

pow fn
signature
pow = fn [base f64, exponent f64] f64

Floating-point power base^exponent.

sin fn
signature
sin = fn [x f64] f64

Sine of x in radians.

Executable Example
veko
use [m = math]
d1 = m/abs-f64 ((m/sin (m/pi / 2.0)) - 1.0)
d2 = m/abs-f64 ((m/cos m/pi) + 1.0)
if d1 > 0.000000000001 or d2 > 0.000000000001 [ err 'sin/cos mismatch' ] else [ .ok ]
cos fn
signature
cos = fn [x f64] f64

Cosine of x in radians.

tan fn
signature
tan = fn [x f64] f64

Tangent of x in radians.

atan fn
signature
atan = fn [x f64] f64

Arctangent of x in radians ([-pi/2, pi/2]).

Executable Example
veko
use [m = math]
d = m/abs-f64 ((m/atan 1.0) - (m/pi / 4.0))
if d > 0.000000000001 [ err 'atan mismatch' ] else [ .ok ]
signature
atan2 = fn [y f64, x f64] f64

Two-argument arctangent atan2(y, x) in radians ([-pi, pi]).