Skip to content

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.

Zynx
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

TypeKindPublic fieldsMethodsDescription
Floatbuiltin marker interfaceN/ANoneImplemented 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).

NameDescription
sqrtSquare root. sqrt of a negative value is NaN.
absAbsolute value (clears the sign bit; abs(-0.0) is 0.0, abs(NaN) is NaN).
floorRounds toward negative infinity.
ceilRounds toward positive infinity.
truncRounds toward zero.
roundRounds to nearest; ties round half away from zero (round(2.5) == 3.0).
rintRounds to nearest; ties round half to even (rint(2.5) == 2.0).
sinSine (radians).
cosCosine (radians).
tanTangent (radians).
expBase-e exponential.
exp2Base-2 exponential.
logNatural logarithm.
log2Base-2 logarithm.
log10Base-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.

NameSignature (scalar)Description
powpow<T: Float>(base: T, exponent: T) -> TRaises base to exponent.
copysigncopysign<T: Float>(magnitude: T, sign: T) -> TMagnitude of the first argument with the sign of the second.
minmin<T: Float>(a: T, b: T) -> TSmaller value; lowers to llvm.minnumNaN loses: if one operand is NaN the other is returned.
maxmax<T: Float>(a: T, b: T) -> TLarger value; lowers to llvm.maxnumNaN loses.
fmafma<T: Float>(a: T, b: T, c: T) -> TFused multiply-add a * b + c with a single rounding.

Integer Functions

NameSignatureDescription
absabs<T: Signed>(x: T) -> TAbsolute value. Traps on the minimum value (for example abs(-128 as i8)), like all checked arithmetic.
minmin<T: Integer>(a: T, b: T) -> TSmaller value.
maxmax<T: Integer>(a: T, b: T) -> TLarger value.

Predicates

NameSignatureDescription
is_nanis_nan<T: Float>(x: T) -> boolTrue when x is NaN.
is_finiteis_finite<T: Float>(x: T) -> boolTrue when x is neither NaN nor an infinity.
is_infis_inf<T: Float>(x: T) -> boolTrue when x is positive or negative infinity.

Constants

All constants are f64 values.

NameValueDescription
PI3.14159265358979323846π
TAU6.28318530717958647692
E2.71828182845904523536Euler's number
LN20.69314718055994530942ln(2)
LN102.30258509299404568402ln(10)
SQRT21.41421356237309504880√2

Full-precision f128 (binary128) constants, 36 significant digits, correctly rounded (values from GCC's quadmath.h):

NameValueDescription
PI_F1283.141592653589793238462643383279502884π
TAU_F1286.283185307179586476925286766559005768
E_F1282.718281828459045235360287471352662498Euler's number
LN2_F1280.693147180559945309417232121458176568ln(2)
LN10_F1282.302585092994045684017991454684364208ln(10)
SQRT2_F1281.414213562373095048801688724209698079√2

Semantics Notes

  • min/max follow minnum/maxnum semantics: NaN loses, so min(NaN, 5.0) == 5.0. This differs from a raw a < b ? a : b comparison.
  • round rounds ties half away from zero; rint rounds ties half to even. rint(2.5) == 2.0 but round(2.5) == 3.0.
  • f128 arithmetic (+ - * /), comparisons, float/integer conversions, and the sign-bit operations abs and copysign are exact IEEE binary128 everywhere, including macOS: the Zynx runtime library bundles soft-float builtins for them on Apple targets (see lib/runtime/compiler_rt/), where the system runtime provides none, and abs/copysign compile to inline bit operations.
  • On Apple targets, every other f128 std.math function — the transcendentals (sin, cos, tan, exp, exp2, log, log2, log10, pow, sqrt), the rounding functions (floor, ceil, trunc, round, rint), min/max, and fma — is computed at f64 precision by design: Apple's long double is plain f64 and its libm has no binary128 entry points, so the compiler lowers these through the f64 path (truncate, compute, extend) instead of emitting *l libcalls with a mismatched ABI. Results are correct f64 values extended to f128 — a documented platform limitation. On Linux these are full binary128.

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.

Released under the MIT License.