zena:math
import {…} from 'zena:math';
Mathematical operations, numeric limits, bit manipulation intrinsics, and wide arithmetic.
Provides constants for integer bounds (I32_MIN, I32_MAX, etc.) and
for PI, E, TAU, LN2, LN10 and SQRT2; bitwise operations
(clz, ctz, popcnt); the floating-point operations Wasm has
instructions for (abs, floor, ceil, sqrt, min, max); the
exponential, logarithmic, trigonometric and hyperbolic functions,
pow, cbrt, hypot and fma, each over f64 and f32; and
128-bit arithmetic (mulWide, add128).
The transcendental functions are a port of musl's libm, which is
FreeBSD's and before that Sun's fdlibm (the math/ directory). Each
is accurate to under 1 ulp unless its documentation says otherwise,
and fma is exact.
Examples ​
import { min, max, sqrt, clz, popcnt, sin, pow, PI } from 'zena:math';
let smallest = min(3.0, 7.5);
let root = sqrt(16.0);
let leadingZeros = clz(16);
let bits = popcnt(7);
let height = sin(PI / 6.0);
let cube = pow(3.0, 3.0);
Functions
isNaN
function isNaN(x: f64): boolean
Whether x is NaN, which x == x cannot tell: NaN is unequal to itself.
isNaN
function isNaN(x: f32): boolean
mulWide
declare function mulWide(x: u64, y: u64): inline (u64, u64)
The full 128-bit unsigned product of two u64s, as (low, high).
mulWideSigned
declare function mulWideSigned(x: i64, y: i64): inline (i64, i64)
The full 128-bit signed product of two i64s, as (low, high).
add128
declare function add128(aLow: u64, aHigh: u64, bLow: u64, bHigh: u64): inline (u64, u64)
128-bit addition of (aLow, aHigh) + (bLow, bHigh), wrapping.
sub128
declare function sub128(aLow: u64, aHigh: u64, bLow: u64, bHigh: u64): inline (u64, u64)
128-bit subtraction of (aLow, aHigh) - (bLow, bHigh), wrapping.
clz
declare function clz(x: i32): i32
ctz
declare function ctz(x: i32): i32
popcnt
declare function popcnt(x: i32): i32
abs
declare function abs(x: f32): f32
neg
declare function neg(x: f32): f32
ceil
declare function ceil(x: f32): f32
floor
declare function floor(x: f32): f32
trunc
declare function trunc(x: f32): f32
nearest
declare function nearest(x: f32): f32
sqrt
declare function sqrt(x: f32): f32
min
declare function min(a: f32, b: f32): f32
max
declare function max(a: f32, b: f32): f32
copysign
declare function copysign(x: f32, y: f32): f32
div
declare function div(a: i32, b: i32): i32
i32_trunc_s
declare function i32_trunc_s(x: f32): i32
i32_trunc_u
declare function i32_trunc_u(x: f32): i32
i64_trunc_s
declare function i64_trunc_s(x: f32): i64
i64_trunc_u
declare function i64_trunc_u(x: f32): i64
i32_reinterpret_f32
declare function i32_reinterpret_f32(x: f32): i32
Reinterpret the bits of an f32 as an i32.
i64_reinterpret_f64
declare function i64_reinterpret_f64(x: f64): i64
Reinterpret the bits of an f64 as an i64.
f32_reinterpret_i32
declare function f32_reinterpret_i32(x: i32): f32
Reinterpret the bits of an i32 as an f32.
f64_reinterpret_i64
declare function f64_reinterpret_i64(x: i64): f64
Reinterpret the bits of an i64 as an f64.
exp
function exp(x: f64): f64
e raised to x. Overflows to infinity above 709.78 and underflows to
zero below -745.13; exp(NaN) is NaN.
expm1
function expm1(x: f64): f64
exp(x) - 1, accurate for small x where exp(x) - 1.0 would cancel.
exp2
function exp2(x: f64): f64
2 raised to x. Overflows to infinity from 1024 and underflows to
zero below -1075; exp2(NaN) is NaN.
pow
function pow(x: f64, y: f64): f64
x raised to y. The special cases follow C's pow: pow(x, 0) and
pow(1, y) are 1 even for NaN, a negative x with a non-integer y
is NaN, and an odd integer y keeps the sign of a negative x.
log
function log(x: f64): f64
The natural logarithm of x. log(0) is negative infinity and log
of a negative number is NaN.
log2
function log2(x: f64): f64
The base 2 logarithm of x.
log10
function log10(x: f64): f64
The base 10 logarithm of x.
log1p
function log1p(x: f64): f64
log(1 + x), accurate for small x where log(1.0 + x) would lose
the low bits of x. log1p(-1) is negative infinity and log1p of a
number below -1 is NaN.
sin
function sin(x: f64): f64
The sine of x, in radians. sin of an infinity or NaN is NaN.
cos
function cos(x: f64): f64
The cosine of x, in radians. cos of an infinity or NaN is NaN.
tan
function tan(x: f64): f64
The tangent of x, in radians. tan of an infinity or NaN is NaN.
asin
function asin(x: f64): f64
The arc sine of x, in [-pi/2, pi/2]. asin of a number outside
[-1, 1] is NaN.
acos
function acos(x: f64): f64
The arc cosine of x, in [0, pi]. acos of a number outside [-1, 1]
is NaN.
atan
function atan(x0: f64): f64
The arc tangent of x, in [-pi/2, pi/2].
atan2
function atan2(y: f64, x: f64): f64
The angle of the point (x, y), in (-pi, pi]: atan(y / x) adjusted
by the quadrant, with the C atan2 conventions for zeros and
infinities (atan2(0, -1) is pi, atan2(-0, -1) is -pi).
sinh
function sinh(x: f64): f64
The hyperbolic sine of x.
cosh
function cosh(x: f64): f64
The hyperbolic cosine of x.
tanh
function tanh(x: f64): f64
The hyperbolic tangent of x, in [-1, 1].
asinh
function asinh(x0: f64): f64
The inverse hyperbolic sine of x.
acosh
function acosh(x: f64): f64
The inverse hyperbolic cosine of x, for x >= 1. acosh of a
number below 1 is NaN.
atanh
function atanh(x: f64): f64
The inverse hyperbolic tangent of x, for x in [-1, 1]. atanh(+-1)
is +-infinity, and atanh of a number outside [-1, 1] is NaN.
cbrt
function cbrt(x: f64): f64
The cube root of x, with the sign of x. The error is under 0.667
ulp.
hypot
function hypot(x0: f64, y0: f64): f64
sqrt(x * x + y * y), without overflow or underflow in the squares.
hypot of an infinity is infinity even when the other argument is
NaN. The error is under 1 ulp.
fma
function fma(x: f64, y: f64, z: f64): f64
x * y + z, rounded once.
Variables
PI
let PI: f64
The ratio of a circle's circumference to its diameter.
E
let E: f64
The base of the natural logarithm.
TAU
let TAU: f64
2 * PI: the circumference of the unit circle.
LN2
let LN2: f64
The natural logarithm of 2.
LN10
let LN10: f64
The natural logarithm of 10.
SQRT2
let SQRT2: f64
The square root of 2.
I32_MIN
let I32_MIN: i32
I32_MAX
let I32_MAX: i32
U32_MIN
let U32_MIN: u32
U32_MAX
let U32_MAX: u32
I64_MIN
let I64_MIN: i64
I64_MAX
let I64_MAX: i64
U64_MIN
let U64_MIN: u64
U64_MAX
let U64_MAX: u64