Math API Reference

Mathematical functions, constants, and trigonometry

Import

U std/math

Constants

NameTypeValue
PIf643.141592653589793
EULERf642.718281828459045
TAUf646.283185307179586

Functions

Basic Arithmetic

FunctionSignatureDescription
absF abs(x: f64) -> f64Absolute value (f64)
abs_i64F abs_i64(x: i64) -> i64Absolute value (i64)
minF min(a: f64, b: f64) -> f64Minimum of two f64
maxF max(a: f64, b: f64) -> f64Maximum of two f64
min_i64F min_i64(a: i64, b: i64) -> i64Minimum of two i64
max_i64F max_i64(a: i64, b: i64) -> i64Maximum of two i64
clampF clamp(x: f64, min_val: f64, max_val: f64) -> f64Clamp to range
clamp_i64F clamp_i64(x: i64, min_val: i64, max_val: i64) -> i64Clamp to range (i64)

Power and Roots

FunctionSignatureDescription
sqrtF sqrt(x: f64) -> f64Square root
powF pow(x: f64, y: f64) -> f64Power (x^y)

Rounding

FunctionSignatureDescription
floorF floor(x: f64) -> f64Round down
ceilF ceil(x: f64) -> f64Round up
roundF round(x: f64) -> f64Round to nearest

Trigonometry

FunctionSignatureDescription
sinF sin(x: f64) -> f64Sine
cosF cos(x: f64) -> f64Cosine
tanF tan(x: f64) -> f64Tangent
asinF asin(x: f64) -> f64Arc sine
acosF acos(x: f64) -> f64Arc cosine
atanF atan(x: f64) -> f64Arc tangent
atan2F atan2(y: f64, x: f64) -> f64Two-argument arc tangent

Logarithmic / Exponential

FunctionSignatureDescription
logF log(x: f64) -> f64Natural logarithm
log10F log10(x: f64) -> f64Base-10 logarithm
log2F log2(x: f64) -> f64Base-2 logarithm
expF exp(x: f64) -> f64Exponential (e^x)

Helpers

FunctionSignatureDescription
deg_to_radF deg_to_rad(degrees: f64) -> f64Degrees to radians
rad_to_degF rad_to_deg(radians: f64) -> f64Radians to degrees
approx_eqF approx_eq(a: f64, b: f64, epsilon: f64) -> i64Approximate equality check

Usage Examples

Basic Calculations

U std/math

F main() -> i64 {
    # Absolute value
    x := abs(-42.5)  # 42.5
    y := abs_i64(-10)  # 10

    # Min/max
    smaller := min(3.5, 7.2)  # 3.5
    larger := max_i64(10, 20)  # 20

    # Clamping
    val := clamp(15.0, 0.0, 10.0)  # 10.0

    0
}

Trigonometry

U std/math

F main() -> i64 {
    # Convert degrees to radians
    angle := deg_to_rad(45.0)

    # Compute sine and cosine
    s := sin(angle)
    c := cos(angle)

    # Pythagorean identity: sin²(x) + cos²(x) = 1
    hyp := sqrt(s * s + c * c)  # ~1.0

    # Inverse trigonometric functions
    radians := asin(0.707)
    degrees := rad_to_deg(radians)

    0
}

Power and Logarithms

U std/math

F main() -> i64 {
    # Exponentiation
    squared := pow(5.0, 2.0)  # 25.0
    cubed := pow(2.0, 3.0)    # 8.0

    # Square root
    root := sqrt(16.0)  # 4.0

    # Natural logarithm
    ln := log(EULER)  # ~1.0

    # Exponential
    result := exp(1.0)  # ~2.718 (EULER)

    # Other logarithms
    log_10 := log10(100.0)  # 2.0
    log_2 := log2(8.0)      # 3.0

    0
}

Rounding Operations

U std/math

F main() -> i64 {
    x := 3.7
    y := 3.2

    a := floor(x)  # 3.0
    b := ceil(x)   # 4.0
    c := round(x)  # 4.0

    d := floor(y)  # 3.0
    e := ceil(y)   # 4.0
    f := round(y)  # 3.0

    0
}

Floating-Point Comparison

U std/math

F main() -> i64 {
    a := 0.1 + 0.2
    b := 0.3

    # Direct comparison may fail due to floating-point precision
    # I a == b { ... }

    # Use approximate equality instead
    epsilon := 0.0001
    I approx_eq(a, b, epsilon) == 1 {
        # Values are approximately equal
    }

    0
}

Practical Example: Distance Calculation

U std/math

# Calculate Euclidean distance between two points
F distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
    dx := x2 - x1
    dy := y2 - y1
    sqrt(dx * dx + dy * dy)
}

F main() -> i64 {
    dist := distance(0.0, 0.0, 3.0, 4.0)  # 5.0
    0
}

Practical Example: Circle Calculations

U std/math

F circle_area(radius: f64) -> f64 {
    PI * radius * radius
}

F circle_circumference(radius: f64) -> f64 {
    TAU * radius  # or 2.0 * PI * radius
}

F main() -> i64 {
    r := 5.0
    area := circle_area(r)
    circ := circle_circumference(r)
    0
}