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.
Constants
Section titled “Constants”| 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 |
Functions
Section titled “Functions”f64_to_bits
Section titled “f64_to_bits”fn f64_to_bits(x: f64) -> u64f64_from_bits
Section titled “f64_from_bits”fn f64_from_bits(b: u64) -> f64math_nan
Section titled “math_nan”fn math_nan() -> f64math_inf
Section titled “math_inf”fn math_inf() -> f64is_nan
Section titled “is_nan”fn is_nan(x: f64) -> boolis_inf
Section titled “is_inf”fn is_inf(x: f64) -> boolis_finite
Section titled “is_finite”fn is_finite(x: f64) -> boolsignbit
Section titled “signbit”fn signbit(x: f64) -> boolabs_i64
Section titled “abs_i64”fn abs_i64(x: i64) -> i64min_i64
Section titled “min_i64”fn min_i64(a: i64, b: i64) -> i64max_i64
Section titled “max_i64”fn max_i64(a: i64, b: i64) -> i64clamp_i64
Section titled “clamp_i64”fn clamp_i64(x: i64, lo: i64, hi: i64) -> i64gcd_i64
Section titled “gcd_i64”fn gcd_i64(a: i64, b: i64) -> i64pow_i64
Section titled “pow_i64”fn pow_i64(base: i64, exp: i64) -> i64Integer power by repeated squaring (wraps on overflow).
isqrt_u64
Section titled “isqrt_u64”fn isqrt_u64(n: u64) -> u64Largest r with r * r <= n (0 for negative n).
fn abs(x: f64) -> f64copysign
Section titled “copysign”fn copysign(x: f64, sign: f64) -> f64fn min(a: f64, b: f64) -> f64fn max(a: f64, b: f64) -> f64fn clamp(x: f64, lo: f64, hi: f64) -> f64fn trunc(x: f64) -> f64fn floor(x: f64) -> f64fn ceil(x: f64) -> f64fn round(x: f64) -> f64Rounds half away from zero.
fn fract(x: f64) -> f64fn fmod(x: f64, y: f64) -> f64Remainder with the sign of x (like C’s fmod).
fn sqrt(x: f64) -> f64fn cbrt(x: f64) -> f64fn hypot(x: f64, y: f64) -> f64fn exp(x: f64) -> f64fn ln(x: f64) -> f64fn log2(x: f64) -> f64fn log10(x: f64) -> f64fn pow(x: f64, y: f64) -> f64fn sinh(x: f64) -> f64fn cosh(x: f64) -> f64fn tanh(x: f64) -> f64fn sin(x: f64) -> f64fn cos(x: f64) -> f64fn tan(x: f64) -> f64fn atan(x: f64) -> f64fn atan2(y: f64, x: f64) -> f64fn asin(x: f64) -> f64fn acos(x: f64) -> f64to_radians
Section titled “to_radians”fn to_radians(deg: f64) -> f64to_degrees
Section titled “to_degrees”fn to_degrees(rad: f64) -> f64