math

The math module provides common mathematical operations. All functions are implemented in pure Conjure with no external dependencies.

import "conjure/math"

Integer Functions (64-bit)

These functions operate on i64 values.

abs

Returns the absolute value of x.

var a = math.abs(-42)   // 42
var b = math.abs(42)    // 42

Signature:

func abs(x i64) i64

min

Returns the smaller of two values.

var smallest = math.min(10, 20)  // 10

Signature:

func min(a i64, b i64) i64

max

Returns the larger of two values.

var largest = math.max(10, 20)  // 20

Signature:

func max(a i64, b i64) i64

clamp

Constrains a value to lie within a range.

var clamped = math.clamp(150, 0, 100)  // 100
var inRange = math.clamp(50, 0, 100)   // 50
var tooLow  = math.clamp(-10, 0, 100)  // 0

Signature:

func clamp(value i64, lo i64, hi i64) i64

sign

Returns the sign of x: -1 if negative, 0 if zero, 1 if positive.

var neg = math.sign(-42)  // -1
var zero = math.sign(0)   // 0
var pos = math.sign(42)   // 1

Signature:

func sign(x i64) i64

Integer Functions (32-bit)

These functions operate on i32 values and have the 32 suffix.

FunctionDescription
abs32(x i32) i32Absolute value
min32(a i32, b i32) i32Minimum of two values
max32(a i32, b i32) i32Maximum of two values
clamp32(value i32, lo i32, hi i32) i32Clamp to range
var a i32 = -10
var b = math.abs32(a)  // 10

Floating-Point Functions (64-bit)

These functions operate on f64 values and have the f suffix.

FunctionDescription
absf(x f64) f64Absolute value
minf(a f64, b f64) f64Minimum of two values
maxf(a f64, b f64) f64Maximum of two values
clampf(value f64, lo f64, hi f64) f64Clamp to range
signf(x f64) f64Sign (-1.0, 0.0, or 1.0)
var velocity = -3.5
var speed = math.absf(velocity)  // 3.5

var normalized = math.clampf(1.5, 0.0, 1.0)  // 1.0

Floating-Point Functions (32-bit)

These functions operate on f32 values and have the f32 suffix.

FunctionDescription
absf32(x f32) f32Absolute value
minf32(a f32, b f32) f32Minimum of two values
maxf32(a f32, b f32) f32Maximum of two values
clampf32(value f32, lo f32, hi f32) f32Clamp to range
var x f32 = -2.5
var y = math.absf32(x)  // 2.5

Example: Smooth Interpolation

import "conjure/math"

// Linear interpolation between a and b
func lerp(a f64, b f64, t f64) f64 {
    var t_clamped = math.clampf(t, 0.0, 1.0)
    return a + (b - a) * t_clamped
}

// Usage
var current = lerp(0.0, 100.0, 0.5)  // 50.0
var start   = lerp(0.0, 100.0, 0.0)  // 0.0
var end     = lerp(0.0, 100.0, 1.0)  // 100.0

Example: Bounding Box Check

import "conjure/math"

func isInBounds(x i32, y i32, width i32, height i32) bool {
    var clampedX = math.clamp32(x, 0, width - 1)
    var clampedY = math.clamp32(y, 0, height - 1)
    return x == clampedX and y == clampedY
}