std.math
std.math provides floating-point math functions over LLVM intrinsics, plus integer abs/min/max, IEEE 754 predicates, and common constants. The float functions are generic over the builtin Float marker interface, so one API covers f16, f32, f64, and f128, and every float function has a vector overload over Vector<T, N> that applies per lane.
How It Works
Float is an auto-imported compiler marker implemented by the primitive floating-point types f16, f32, f64, and f128. Integer types do not implement Float.
The float helpers lower to LLVM floating-point intrinsics (llvm.sqrt.f64, llvm.pow.v4f32, ...). Scalar calls compile to hardware instructions or libm calls depending on the target and width; vector calls lower to the vector forms of the same intrinsics.
The integer overloads are plain checked Zynx code: abs<T: Signed> traps on the minimum value (negating it overflows), matching the language's checked arithmetic policy. min/max over Integer are simple comparisons.
The helpers do not allocate and do not throw.
import std.math;
fn main() {
assert math.sqrt(9.0) == 3.0;
assert math.pow(2.0, 10.0) == 1024.0;
assert math.min(1.0, 2.0) == 1.0;
assert math.PI > 3.14159 && math.PI < 3.14160;
let v: Vector<f32, 4> = [1.0, 4.0, 9.0, 16.0];
let r = math.sqrt(v);
assert r[3] == 4.0;
}API Reference
Builtin Numeric Interfaces
| Type | Kind | Public fields | Methods | Description |
|---|---|---|---|---|
Float | builtin marker interface | N/A | None | Implemented by f16, f32, f64, and f128. |
Unary Float Functions
Each function has the scalar form name<T: Float>(x: T) -> T and the vector form name<T: Float, N: usize>(v: Vector<T, N>) -> Vector<T, N> (per-lane).
| Name | Description |
|---|---|
sqrt | Square root. sqrt of a negative value is NaN. |
abs | Absolute value (clears the sign bit; abs(-0.0) is 0.0, abs(NaN) is NaN). |
floor | Rounds toward negative infinity. |
ceil | Rounds toward positive infinity. |
trunc | Rounds toward zero. |
round | Rounds to nearest; ties round half away from zero (round(2.5) == 3.0). |
rint | Rounds to nearest; ties round half to even (rint(2.5) == 2.0). |
sin | Sine (radians). |
cos | Cosine (radians). |
tan | Tangent (radians). |
exp | Base-e exponential. |
exp2 | Base-2 exponential. |
log | Natural logarithm. |
log2 | Base-2 logarithm. |
log10 | Base-10 logarithm. |
Binary And Ternary Float Functions
Scalar forms take matching T: Float arguments; vector forms take matching Vector<T, N> arguments and operate per lane.
| Name | Signature (scalar) | Description |
|---|---|---|
pow | pow<T: Float>(base: T, exponent: T) -> T | Raises base to exponent. |
copysign | copysign<T: Float>(magnitude: T, sign: T) -> T | Magnitude of the first argument with the sign of the second. |
min | min<T: Float>(a: T, b: T) -> T | Smaller value; lowers to llvm.minnum — NaN loses: if one operand is NaN the other is returned. |
max | max<T: Float>(a: T, b: T) -> T | Larger value; lowers to llvm.maxnum — NaN loses. |
fma | fma<T: Float>(a: T, b: T, c: T) -> T | Fused multiply-add a * b + c with a single rounding. |
Integer Functions
| Name | Signature | Description |
|---|---|---|
abs | abs<T: Signed>(x: T) -> T | Absolute value. Traps on the minimum value (for example abs(-128 as i8)), like all checked arithmetic. |
min | min<T: Integer>(a: T, b: T) -> T | Smaller value. |
max | max<T: Integer>(a: T, b: T) -> T | Larger value. |
Predicates
| Name | Signature | Description |
|---|---|---|
is_nan | is_nan<T: Float>(x: T) -> bool | True when x is NaN. |
is_finite | is_finite<T: Float>(x: T) -> bool | True when x is neither NaN nor an infinity. |
is_inf | is_inf<T: Float>(x: T) -> bool | True when x is positive or negative infinity. |
Constants
All constants are f64 values.
| Name | Value | Description |
|---|---|---|
PI | 3.14159265358979323846 | π |
TAU | 6.28318530717958647692 | 2π |
E | 2.71828182845904523536 | Euler's number |
LN2 | 0.69314718055994530942 | ln(2) |
LN10 | 2.30258509299404568402 | ln(10) |
SQRT2 | 1.41421356237309504880 | √2 |
Full-precision f128 (binary128) constants, 36 significant digits, correctly rounded (values from GCC's quadmath.h):
| Name | Value | Description |
|---|---|---|
PI_F128 | 3.141592653589793238462643383279502884 | π |
TAU_F128 | 6.283185307179586476925286766559005768 | 2π |
E_F128 | 2.718281828459045235360287471352662498 | Euler's number |
LN2_F128 | 0.693147180559945309417232121458176568 | ln(2) |
LN10_F128 | 2.302585092994045684017991454684364208 | ln(10) |
SQRT2_F128 | 1.414213562373095048801688724209698079 | √2 |
Semantics Notes
min/maxfollowminnum/maxnumsemantics: NaN loses, somin(NaN, 5.0) == 5.0. This differs from a rawa < b ? a : bcomparison.roundrounds ties half away from zero;rintrounds ties half to even.rint(2.5) == 2.0butround(2.5) == 3.0.f128arithmetic (+ - * /), comparisons, float/integer conversions, and the sign-bit operationsabsandcopysignare exact IEEE binary128 everywhere, including macOS: the Zynx runtime library bundles soft-float builtins for them on Apple targets (seelib/runtime/compiler_rt/), where the system runtime provides none, andabs/copysigncompile to inline bit operations.- On Apple targets, every other
f128std.mathfunction — the transcendentals (sin,cos,tan,exp,exp2,log,log2,log10,pow,sqrt), the rounding functions (floor,ceil,trunc,round,rint),min/max, andfma— is computed atf64precision by design: Apple'slong doubleis plainf64and its libm has no binary128 entry points, so the compiler lowers these through thef64path (truncate, compute, extend) instead of emitting*llibcalls with a mismatched ABI. Results are correctf64values extended tof128— a documented platform limitation. On Linux these are full binary128.
Related Modules
Use std.arith for explicit integer overflow policies and std.bit for integer bit operations.
Status
std.math is the current public float math API. For the full exported surface, see Standard Library Inventory.