zena:math

zena
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 ​

zena
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

zena
function isNaN(x: f64): boolean

Whether x is NaN, which x == x cannot tell: NaN is unequal to itself.

isNaN

zena
function isNaN(x: f32): boolean

mulWide

zena
declare function mulWide(x: u64, y: u64): inline (u64, u64)
Re-exported from zena:math/intrinsics.zena

The full 128-bit unsigned product of two u64s, as (low, high).

mulWideSigned

zena
declare function mulWideSigned(x: i64, y: i64): inline (i64, i64)
Re-exported from zena:math/intrinsics.zena

The full 128-bit signed product of two i64s, as (low, high).

add128

zena
declare function add128(aLow: u64, aHigh: u64, bLow: u64, bHigh: u64): inline (u64, u64)
Re-exported from zena:math/intrinsics.zena

128-bit addition of (aLow, aHigh) + (bLow, bHigh), wrapping.

sub128

zena
declare function sub128(aLow: u64, aHigh: u64, bLow: u64, bHigh: u64): inline (u64, u64)
Re-exported from zena:math/intrinsics.zena

128-bit subtraction of (aLow, aHigh) - (bLow, bHigh), wrapping.

clz

zena
declare function clz(x: i32): i32
Re-exported from zena:math/intrinsics.zena

ctz

zena
declare function ctz(x: i32): i32
Re-exported from zena:math/intrinsics.zena

popcnt

zena
declare function popcnt(x: i32): i32
Re-exported from zena:math/intrinsics.zena

abs

zena
declare function abs(x: f32): f32
Re-exported from zena:math/intrinsics.zena

neg

zena
declare function neg(x: f32): f32
Re-exported from zena:math/intrinsics.zena

ceil

zena
declare function ceil(x: f32): f32
Re-exported from zena:math/intrinsics.zena

floor

zena
declare function floor(x: f32): f32
Re-exported from zena:math/intrinsics.zena

trunc

zena
declare function trunc(x: f32): f32
Re-exported from zena:math/intrinsics.zena

nearest

zena
declare function nearest(x: f32): f32
Re-exported from zena:math/intrinsics.zena

sqrt

zena
declare function sqrt(x: f32): f32
Re-exported from zena:math/intrinsics.zena

min

zena
declare function min(a: f32, b: f32): f32
Re-exported from zena:math/intrinsics.zena

max

zena
declare function max(a: f32, b: f32): f32
Re-exported from zena:math/intrinsics.zena

copysign

zena
declare function copysign(x: f32, y: f32): f32
Re-exported from zena:math/intrinsics.zena

div

zena
declare function div(a: i32, b: i32): i32
Re-exported from zena:math/intrinsics.zena

i32_trunc_s

zena
declare function i32_trunc_s(x: f32): i32
Re-exported from zena:math/intrinsics.zena

i32_trunc_u

zena
declare function i32_trunc_u(x: f32): i32
Re-exported from zena:math/intrinsics.zena

i64_trunc_s

zena
declare function i64_trunc_s(x: f32): i64
Re-exported from zena:math/intrinsics.zena

i64_trunc_u

zena
declare function i64_trunc_u(x: f32): i64
Re-exported from zena:math/intrinsics.zena

i32_reinterpret_f32

zena
declare function i32_reinterpret_f32(x: f32): i32
Re-exported from zena:math/intrinsics.zena

Reinterpret the bits of an f32 as an i32.

i64_reinterpret_f64

zena
declare function i64_reinterpret_f64(x: f64): i64
Re-exported from zena:math/intrinsics.zena

Reinterpret the bits of an f64 as an i64.

f32_reinterpret_i32

zena
declare function f32_reinterpret_i32(x: i32): f32
Re-exported from zena:math/intrinsics.zena

Reinterpret the bits of an i32 as an f32.

f64_reinterpret_i64

zena
declare function f64_reinterpret_i64(x: i64): f64
Re-exported from zena:math/intrinsics.zena

Reinterpret the bits of an i64 as an f64.

exp

zena
function exp(x: f64): f64
Re-exported from zena:math/exp.zena

e raised to x. Overflows to infinity above 709.78 and underflows to zero below -745.13; exp(NaN) is NaN.

expm1

zena
function expm1(x: f64): f64
Re-exported from zena:math/exp.zena

exp(x) - 1, accurate for small x where exp(x) - 1.0 would cancel.

exp2

zena
function exp2(x: f64): f64
Re-exported from zena:math/pow.zena

2 raised to x. Overflows to infinity from 1024 and underflows to zero below -1075; exp2(NaN) is NaN.

pow

zena
function pow(x: f64, y: f64): f64
Re-exported from zena:math/pow.zena

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

zena
function log(x: f64): f64
Re-exported from zena:math/log.zena

The natural logarithm of x. log(0) is negative infinity and log of a negative number is NaN.

log2

zena
function log2(x: f64): f64
Re-exported from zena:math/log.zena

The base 2 logarithm of x.

log10

zena
function log10(x: f64): f64
Re-exported from zena:math/log.zena

The base 10 logarithm of x.

log1p

zena
function log1p(x: f64): f64
Re-exported from zena:math/log.zena

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

zena
function sin(x: f64): f64
Re-exported from zena:math/trig.zena

The sine of x, in radians. sin of an infinity or NaN is NaN.

cos

zena
function cos(x: f64): f64
Re-exported from zena:math/trig.zena

The cosine of x, in radians. cos of an infinity or NaN is NaN.

tan

zena
function tan(x: f64): f64
Re-exported from zena:math/trig.zena

The tangent of x, in radians. tan of an infinity or NaN is NaN.

asin

zena
function asin(x: f64): f64
Re-exported from zena:math/inverse-trig.zena

The arc sine of x, in [-pi/2, pi/2]. asin of a number outside [-1, 1] is NaN.

acos

zena
function acos(x: f64): f64
Re-exported from zena:math/inverse-trig.zena

The arc cosine of x, in [0, pi]. acos of a number outside [-1, 1] is NaN.

atan

zena
function atan(x0: f64): f64
Re-exported from zena:math/inverse-trig.zena

The arc tangent of x, in [-pi/2, pi/2].

atan2

zena
function atan2(y: f64, x: f64): f64
Re-exported from zena:math/inverse-trig.zena

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

zena
function sinh(x: f64): f64
Re-exported from zena:math/hyperbolic.zena

The hyperbolic sine of x.

cosh

zena
function cosh(x: f64): f64
Re-exported from zena:math/hyperbolic.zena

The hyperbolic cosine of x.

tanh

zena
function tanh(x: f64): f64
Re-exported from zena:math/hyperbolic.zena

The hyperbolic tangent of x, in [-1, 1].

asinh

zena
function asinh(x0: f64): f64
Re-exported from zena:math/hyperbolic.zena

The inverse hyperbolic sine of x.

acosh

zena
function acosh(x: f64): f64
Re-exported from zena:math/hyperbolic.zena

The inverse hyperbolic cosine of x, for x >= 1. acosh of a number below 1 is NaN.

atanh

zena
function atanh(x: f64): f64
Re-exported from zena:math/hyperbolic.zena

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

zena
function cbrt(x: f64): f64
Re-exported from zena:math/roots.zena

The cube root of x, with the sign of x. The error is under 0.667 ulp.

hypot

zena
function hypot(x0: f64, y0: f64): f64
Re-exported from zena:math/roots.zena

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

zena
function fma(x: f64, y: f64, z: f64): f64
Re-exported from zena:math/fma.zena

x * y + z, rounded once.

Variables

PI

zena
let PI: f64

The ratio of a circle's circumference to its diameter.

E

zena
let E: f64

The base of the natural logarithm.

TAU

zena
let TAU: f64

2 * PI: the circumference of the unit circle.

LN2

zena
let LN2: f64

The natural logarithm of 2.

LN10

zena
let LN10: f64

The natural logarithm of 10.

SQRT2

zena
let SQRT2: f64

The square root of 2.

I32_MIN

zena
let I32_MIN: i32
Re-exported from zena:math/intrinsics.zena

I32_MAX

zena
let I32_MAX: i32
Re-exported from zena:math/intrinsics.zena

U32_MIN

zena
let U32_MIN: u32
Re-exported from zena:math/intrinsics.zena

U32_MAX

zena
let U32_MAX: u32
Re-exported from zena:math/intrinsics.zena

I64_MIN

zena
let I64_MIN: i64
Re-exported from zena:math/intrinsics.zena

I64_MAX

zena
let I64_MAX: i64
Re-exported from zena:math/intrinsics.zena

U64_MIN

zena
let U64_MIN: u64
Re-exported from zena:math/intrinsics.zena

U64_MAX

zena
let U64_MAX: u64
Re-exported from zena:math/intrinsics.zena