Skip to content

Math

Source: std/math.mw.

Everything is implemented in Marrow (no libm): sqrt, exp, ln, pow, the trigonometric functions and their inverses are accurate to a few ulps, and edge cases (NaN, infinities, signed zeros) follow IEEE 754.

Math library in plain Marrow (no libm). Results are accurate to a few ulps for ordinary arguments. Trigonometric functions reduce their argument with a three-part constant (Cody-Waite), which is accurate for |x| below about 1e6; larger arguments still give a result, with fewer correct digits.

Name Type Value Notes
PI f64 3.141592653589793
TAU f64 6.283185307179586
HALF_PI f64 1.5707963267948966
E f64 2.718281828459045
LN2 f64 0.6931471805599453
LN10 f64 2.302585092994046
SQRT2 f64 1.4142135623730951
fn f64_to_bits(x: f64) -> u64
fn f64_from_bits(b: u64) -> f64
fn math_nan() -> f64
fn math_inf() -> f64
fn is_nan(x: f64) -> bool
fn is_inf(x: f64) -> bool
fn is_finite(x: f64) -> bool
fn signbit(x: f64) -> bool
fn abs_i64(x: i64) -> i64
fn min_i64(a: i64, b: i64) -> i64
fn max_i64(a: i64, b: i64) -> i64
fn clamp_i64(x: i64, lo: i64, hi: i64) -> i64
fn gcd_i64(a: i64, b: i64) -> i64
fn pow_i64(base: i64, exp: i64) -> i64

Integer power by repeated squaring (wraps on overflow).

fn isqrt_u64(n: u64) -> u64

Largest r with r * r <= n (0 for negative n).

fn abs(x: f64) -> f64
fn copysign(x: f64, sign: f64) -> f64
fn min(a: f64, b: f64) -> f64
fn max(a: f64, b: f64) -> f64
fn clamp(x: f64, lo: f64, hi: f64) -> f64
fn trunc(x: f64) -> f64
fn floor(x: f64) -> f64
fn ceil(x: f64) -> f64
fn round(x: f64) -> f64

Rounds half away from zero.

fn fract(x: f64) -> f64
fn fmod(x: f64, y: f64) -> f64

Remainder with the sign of x (like C’s fmod).

fn sqrt(x: f64) -> f64
fn cbrt(x: f64) -> f64
fn hypot(x: f64, y: f64) -> f64
fn exp(x: f64) -> f64
fn ln(x: f64) -> f64
fn log2(x: f64) -> f64
fn log10(x: f64) -> f64
fn pow(x: f64, y: f64) -> f64
fn sinh(x: f64) -> f64
fn cosh(x: f64) -> f64
fn tanh(x: f64) -> f64
fn sin(x: f64) -> f64
fn cos(x: f64) -> f64
fn tan(x: f64) -> f64
fn atan(x: f64) -> f64
fn atan2(y: f64, x: f64) -> f64
fn asin(x: f64) -> f64
fn acos(x: f64) -> f64
fn to_radians(deg: f64) -> f64
fn to_degrees(rad: f64) -> f64