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) // 42Signature:
func abs(x i64) i64min
Returns the smaller of two values.
var smallest = math.min(10, 20) // 10Signature:
func min(a i64, b i64) i64max
Returns the larger of two values.
var largest = math.max(10, 20) // 20Signature:
func max(a i64, b i64) i64clamp
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) // 0Signature:
func clamp(value i64, lo i64, hi i64) i64sign
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) // 1Signature:
func sign(x i64) i64Integer Functions (32-bit)
These functions operate on i32 values and have the 32 suffix.
| Function | Description |
|---|---|
abs32(x i32) i32 | Absolute value |
min32(a i32, b i32) i32 | Minimum of two values |
max32(a i32, b i32) i32 | Maximum of two values |
clamp32(value i32, lo i32, hi i32) i32 | Clamp to range |
var a i32 = -10
var b = math.abs32(a) // 10Floating-Point Functions (64-bit)
These functions operate on f64 values and have the f suffix.
| Function | Description |
|---|---|
absf(x f64) f64 | Absolute value |
minf(a f64, b f64) f64 | Minimum of two values |
maxf(a f64, b f64) f64 | Maximum of two values |
clampf(value f64, lo f64, hi f64) f64 | Clamp to range |
signf(x f64) f64 | Sign (-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.0Floating-Point Functions (32-bit)
These functions operate on f32 values and have the f32 suffix.
| Function | Description |
|---|---|
absf32(x f32) f32 | Absolute value |
minf32(a f32, b f32) f32 | Minimum of two values |
maxf32(a f32, b f32) f32 | Maximum of two values |
clampf32(value f32, lo f32, hi f32) f32 | Clamp to range |
var x f32 = -2.5
var y = math.absf32(x) // 2.5Example: 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.0Example: 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
}