Skip to content

Std\Math

Math — polymorphic numeric operations and float math.

The Num trait provides polymorphic abs, max, min for both Int and Float. Float-specific functions (sqrt, sin, cos, etc.) are bound from the C math library via extern declarations.

trait Num a
abs : a -> a
max : a -> a -> a
min : a -> a -> a
negate : a -> a
end

Numeric trait — polymorphic over Int and Float.

Restricts a value to the range [lo, hi].

clamp 0 10 15 # => 10

Returns 1 for positive, -1 for negative, 0 for zero.

sign 42 # => 1

Returns true if the integer is even.

Returns true if the integer is odd.

Greatest common divisor (Euclidean algorithm).

gcd 12 8 # => 4

Integer exponentiation. Uses fast squaring.

pow 2 10 # => 1024

Factorial: n! = 1 * 2 * ... * n.

factorial 5 # => 120

Square root (Float -> Float).

Sine (Float -> Float, radians).

Cosine (Float -> Float, radians).

Tangent (Float -> Float, radians).

Natural logarithm (Float -> Float).

Exponential e^x (Float -> Float).

Floor (Float -> Float).

Ceiling (Float -> Float).

Round to nearest integer (Float -> Float).

Pi constant.

pi # => 3.14159265358979