Skip to reference

std.math.float

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.math.float — floating-point numerics over f64: predicates, decomposition, rounding, and the transcendentals. Stability tier: core.

The pure-wolf decision (sc04's Target 2, D31 applied). Every function here is implemented in ordinary wolf — range reduction plus polynomial evaluation — and NOT delegated to a platform libm. The reason is differential testability: a libm binding would make sin(1.0) mean whatever the host's C library means, which no interpreter can reproduce bit-for-bit and no two operating systems need agree on. Pure wolf makes the answer a property of the SOURCE: every implementation that evaluates IEEE-754 binary64 arithmetic correctly gets the same bits, and the tests pin those bits as exact literals. The accuracy contract below is the price, and it is stated per function rather than assumed.

The accuracy contract. Every algebraic and transcendental function documents a bound in ulp (units in the last place of the correctly rounded result), measured over the pinned reference set in tests/ulp/reference.toml and enforced by cargo xtask ulp. The bound is a measurement, not an aspiration: where a function does not reach 2 ulp on some input class, the function says so. Faithful rounding (≤ 1 ulp) is NOT claimed anywhere, and correctly-rounded (≤ 0.5 ulp) is not a v1 promise for any transcendental — the s37 intrinsics layer is where a hardware sqrt and friends would earn those, and this source stays the semantic reference either way (F-0028).

Why the constants are functions. pi(), nan(), f_max() and the rest would be const items; a module-level const is unsupported when USED under wolfc's checked tier and when DECLARED on the native rung, and the native rung is the only compiler-side lane that executes float code at all (the checked tier refuses float literals outright). Constants-as-functions keep the whole module executing on two implementations. Filed as F-0025.

**Never write x != y on floats in this module.** The native backend lowers != on f64 to an ORDERED comparison, so nan != nan is false there and true everywhere else (F-0027). Every inequality here is spelled !(x == y), which agrees on all three lanes.

Not here, by contract: float parsing and printing (sc05 owns the decimal boundary; this module ships only the binary to_bits currency), statistics, complex, decimal, and any SIMD or intrinsic fast path.

Types and traits

No public declarations in this section.

Functions and methods

pi

fn · Source line 55

pub fn pi() -> f64

π, the ratio of a circle's circumference to its diameter, rounded to the nearest f64. Total: no error row, no traps.

float.pi() == 3.141592653589793
float.pi() > 3.14

tau

fn · Source line 65

pub fn tau() -> f64

τ = 2π. Total: no error row, no traps.

float.tau() == 6.283185307179586
float.tau() == float.pi() * 2.0

e

fn · Source line 76

pub fn e() -> f64

Euler's number, the base of the natural logarithm. Total: no error row, no traps.

float.e() == 2.718281828459045
float.e() > 2.7

inf

fn · Source line 87

pub fn inf() -> f64

Positive infinity. Total: no error row, no traps — float division by zero is IEEE, not the checked-arithmetic fault X3 gives integers.

float.inf() > 1.0e308
float.inf() == 1.0 / 0.0

neg_inf

fn · Source line 97

pub fn neg_inf() -> f64

Negative infinity. Total: no error row, no traps.

float.neg_inf() < -1.0e308
float.neg_inf() == -float.inf()

nan

fn · Source line 108

pub fn nan() -> f64

A quiet NaN. Total: no error row, no traps. NaN is the one f64 unequal to itself — probe it with is_nan, never with ==.

float.is_nan(float.nan()) == true
float.is_nan(0.0) == false

epsilon

fn · Source line 119

pub fn epsilon() -> f64

The difference between 1.0 and the next representable f64 (2^-52). Total: no error row, no traps.

float.epsilon() == 2.220446049250313e-16
1.0 + float.epsilon() > 1.0

f_min

fn · Source line 131

pub fn f_min() -> f64

The least finite f64 — the most negative one, not the smallest positive one (that is f_min_positive, a nursery candidate this module names and does not ship). Total: no error row, no traps.

float.f_min() == -float.f_max()
float.f_min() < -1.0e308

f_max

fn · Source line 141

pub fn f_max() -> f64

The greatest finite f64. Total: no error row, no traps.

float.f_max() == 1.7976931348623157e308
float.f_max() < float.inf()

is_nan

fn · Source line 155

pub fn is_nan(x: f64) -> bool

True exactly for NaN. Total: no error row, no traps.

The body is !(x == x) rather than the textbook x != x: see the module header (F-0027).

float.is_nan(0.0 / 0.0) == true
float.is_nan(1.0) == false
float.is_nan(float.inf()) == false

is_inf

fn · Source line 167

pub fn is_inf(x: f64) -> bool

True for either infinity. Total: no error row, no traps.

float.is_inf(float.inf()) == true
float.is_inf(float.neg_inf()) == true
float.is_inf(1.0e308) == false
float.is_inf(float.nan()) == false

is_finite

fn · Source line 183

pub fn is_finite(x: f64) -> bool

True for everything that is neither NaN nor an infinity. Total: no error row, no traps.

float.is_finite(1.0) == true
float.is_finite(0.0) == true
float.is_finite(float.inf()) == false
float.is_finite(float.nan()) == false

is_sign_negative

fn · Source line 200

pub fn is_sign_negative(x: f64) -> bool

True when x carries a negative sign bit — which includes -0.0, the value x < 0.0 cannot see. NaN's sign is not observable at these pins and answers false. Total: no error row, no traps.

float.is_sign_negative(-1.0) == true
float.is_sign_negative(-0.0) == true
float.is_sign_negative(0.0) == false
float.is_sign_negative(1.0) == false

abs

fn · Source line 224

pub fn abs(x: f64) -> f64

Magnitude. abs(-0.0) is +0.0 and abs(NaN) is NaN. Total: no error row, no traps.

float.abs(-2.5) == 2.5
float.abs(2.5) == 2.5
float.abs(-0.0) == 0.0
float.is_sign_negative(float.abs(-0.0)) == false

signum

fn · Source line 251

pub fn signum(x: f64) -> f64

The sign of x as a float: 1.0, -1.0, the zero itself for ±0.0 (sign preserved), and NaN for NaN. Total: no error row, no traps.

The zero and NaN answers are the documented choice: signum is sign EXTRACTION, and a zero's sign is a zero's, not a one's.

float.signum(3.0) == 1.0
float.signum(-3.0) == -1.0
float.signum(0.0) == 0.0
float.is_nan(float.signum(float.nan())) == true

copysign

fn · Source line 273

pub fn copysign(x: f64, y: f64) -> f64

x with the sign of y. copysign(3.0, -0.0) is -3.0: the sign of a zero counts. Total: no error row, no traps.

float.copysign(3.0, -1.0) == -3.0
float.copysign(-3.0, 1.0) == 3.0
float.copysign(3.0, -0.0) == -3.0
float.copysign(-0.0, 1.0) == 0.0

trunc

fn · Source line 291

pub fn trunc(x: f64) -> f64

Truncation toward zero: the integral part, with the sign of x (trunc(-0.5) is -0.0). Infinities and NaN pass through. Total: no error row, no traps.

float.trunc(2.7) == 2.0
float.trunc(-2.7) == -2.0
float.trunc(2.0) == 2.0
float.trunc(0.5) == 0.0

floor

fn · Source line 327

pub fn floor(x: f64) -> f64

The greatest integer at or below x. Infinities and NaN pass through. Total: no error row, no traps.

float.floor(2.7) == 2.0
float.floor(-2.7) == -3.0
float.floor(2.0) == 2.0
float.floor(-0.5) == -1.0

ceil

fn · Source line 346

pub fn ceil(x: f64) -> f64

The least integer at or above x. ceil(-0.5) is -0.0. Infinities and NaN pass through. Total: no error row, no traps.

float.ceil(2.1) == 3.0
float.ceil(-2.1) == -2.0
float.ceil(2.0) == 2.0
float.ceil(0.5) == 1.0

round

fn · Source line 369

pub fn round(x: f64) -> f64

Rounds to the nearest integer, halves away from zero — the schoolbook rule, not the banker's rounding IEEE arithmetic itself uses. round(0.5) is 1.0 and round(-0.5) is -1.0, where round-half-to-even would answer 0.0 and -0.0. Infinities and NaN pass through. Total: no error row, no traps.

float.round(0.5) == 1.0
float.round(-0.5) == -1.0
float.round(2.5) == 3.0
float.round(2.4) == 2.0
float.round(-2.6) == -3.0

fract

fn · Source line 398

pub fn fract(x: f64) -> f64

The fractional part: x - trunc(x), carrying the sign of x. fract of an infinity is NaN (infinity has no fractional part to name), and NaN passes through. Total: no error row, no traps.

float.fract(2.75) == 0.75
float.fract(-2.75) == -0.75
float.fract(3.0) == 0.0
float.is_nan(float.fract(float.inf())) == true

sqrt

fn · Source line 442

pub fn sqrt(x: f64) -> f64

The positive square root. sqrt of a negative number is NaN, of ±0.0 is that zero, of +inf is +inf, of NaN is NaN.

Accuracy: ≤ 1 ulp over the reference set — five Newton steps from a linear seed on the reduced argument, with the power-of-four scaling exact. Total: no error row, no traps.

float.sqrt(4.0) == 2.0
float.sqrt(0.0) == 0.0
float.sqrt(1.0) == 1.0
float.is_nan(float.sqrt(-1.0)) == true
float.sqrt(float.inf()) == float.inf()

cbrt

fn · Source line 489

pub fn cbrt(x: f64) -> f64

The real cube root, defined for negative arguments too (unlike powf(x, 1.0 / 3.0)). cbrt(±0.0) is that zero, cbrt(±inf) is that infinity, cbrt(NaN) is NaN.

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.cbrt(8.0) == 2.0
float.cbrt(-8.0) == -2.0
float.cbrt(0.0) == 0.0
float.cbrt(1.0) == 1.0

hypot

fn · Source line 540

pub fn hypot(a: f64, b: f64) -> f64

sqrt(a*a + b*b) computed without the intermediate overflow the literal formula suffers. hypot of an infinity is +inf even when the other argument is NaN (IEEE's rule: the magnitude is already known).

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.hypot(3.0, 4.0) == 5.0
float.hypot(0.0, 0.0) == 0.0
float.hypot(-3.0, -4.0) == 5.0
float.hypot(float.inf(), float.nan()) == float.inf()

exp

fn · Source line 584

pub fn exp(x: f64) -> f64

e raised to the power x. exp(-inf) is 0.0, exp(+inf) is +inf, exp(NaN) is NaN; arguments above about 709.78 overflow to +inf and below about -745.13 underflow to 0.0, exactly as the range demands.

Accuracy: ≤ 2 ulp over the reference set, subnormal results included — the final scaling is staged so that a subnormal answer rounds once rather than once per halving (scale2). Total: no error row, no traps.

float.exp(0.0) == 1.0
float.exp(1.0) == 2.7182818284590455
float.exp(float.neg_inf()) == 0.0
float.exp(float.inf()) == float.inf()

exp2

fn · Source line 648

pub fn exp2(x: f64) -> f64

2.0 raised to the power x, exact at every integral x in range.

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.exp2(0.0) == 1.0
float.exp2(10.0) == 1024.0
float.exp2(-1.0) == 0.5
float.exp2(0.5) == 1.414213562373095

ln

fn · Source line 684

pub fn ln(x: f64) -> f64

The natural logarithm. ln(0.0) is -inf, ln of a negative number is NaN, ln(+inf) is +inf, ln(1.0) is exactly 0.0.

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.ln(1.0) == 0.0
float.ln(float.e()) == 1.0
float.ln(0.0) == float.neg_inf()
float.is_nan(float.ln(-1.0)) == true

log2

fn · Source line 751

pub fn log2(x: f64) -> f64

The base-2 logarithm, exact at every power of two. log2(0.0) is -inf and log2 of a negative number is NaN.

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.log2(1.0) == 0.0
float.log2(8.0) == 3.0
float.log2(0.5) == -1.0
float.log2(0.0) == float.neg_inf()

log10

fn · Source line 785

pub fn log10(x: f64) -> f64

The base-10 logarithm. log10(0.0) is -inf and log10 of a negative number is NaN.

Accuracy: ≤ 2 ulp over the reference set. Exactness at powers of ten is NOT promised — log10(1000.0) is the correctly rounded neighbour of 3, not 3 — because the base-ten exponent is not a term this reduction can separate. Total: no error row, no traps.

float.log10(1.0) == 0.0
float.log10(10.0) == 1.0
float.log10(0.0) == float.neg_inf()
float.log10(100.0) == 2.0

sin

fn · Source line 868

pub fn sin(x: f64) -> f64

The sine of x in radians. NaN in, NaN out; an infinite argument has no meaningful sine and answers NaN.

Accuracy: ≤ 2 ulp over the reference set for |x| ≤ 2^20. Beyond that the three-piece argument reduction runs out of bits and the error grows with |x| — the documented limit, not a hidden one. Total: no error row, no traps.

float.sin(0.0) == 0.0
float.sin(1.0) == 0.8414709848078965
float.is_nan(float.sin(float.inf())) == true
float.abs(float.sin(float.pi())) < 1.0e-15

cos

fn · Source line 904

pub fn cos(x: f64) -> f64

The cosine of x in radians. NaN in, NaN out; an infinite argument answers NaN.

Accuracy: ≤ 2 ulp over the reference set for |x| ≤ 2^20, with the same reduction limit sin documents. Total: no error row, no traps.

float.cos(0.0) == 1.0
float.cos(1.0) == 0.5403023058681398
float.is_nan(float.cos(float.inf())) == true
float.cos(-1.0) == float.cos(1.0)

tan

fn · Source line 940

pub fn tan(x: f64) -> f64

The tangent of x in radians, as sin(x) / cos(x) over one shared reduction. NaN in, NaN out; an infinite argument answers NaN.

Accuracy: ≤ 2 ulp over the reference set. The quotient inherits both polynomials' error, and near a pole the cancellation in cos is genuine — the reference set includes tan(pi/2), whose enormous answer is the correctly rounded one for the argument actually given. Total: no error row, no traps.

float.tan(0.0) == 0.0
float.tan(1.0) == 1.557407724654902
float.is_nan(float.tan(float.inf())) == true
float.abs(float.tan(float.pi() * 0.25) - 1.0) < 1.0e-15

atan

fn · Source line 1014

pub fn atan(x: f64) -> f64

The arctangent, in [-pi/2, pi/2]. atan(±inf) is ±pi/2, NaN in NaN out.

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.atan(0.0) == 0.0
float.atan(1.0) == 0.7853981633974483
float.atan(-1.0) == -0.7853981633974483
float.atan(float.inf()) == 1.5707963267948966

asin

fn · Source line 1047

pub fn asin(x: f64) -> f64

The arcsine, in [-pi/2, pi/2]. An argument outside [-1, 1] is NaN — the value has no real arcsine, and a row would make every caller of a total function handle a case the domain already states.

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.asin(0.0) == 0.0
float.asin(1.0) == 1.5707963267948966
float.asin(-1.0) == -1.5707963267948966
float.is_nan(float.asin(2.0)) == true

acos

fn · Source line 1090

pub fn acos(x: f64) -> f64

The arccosine, in [0, pi]. An argument outside [-1, 1] is NaN.

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.acos(1.0) == 0.0
float.acos(0.0) == 1.5707963267948966
float.acos(-1.0) == 3.141592653589793
float.is_nan(float.acos(-2.0)) == true

atan2

fn · Source line 1126

pub fn atan2(y: f64, x: f64) -> f64

The angle of the point (x, y) from the positive x-axis, in (-pi, pi], with the quadrant taken from both signs. The IEEE special cases are all here and all documented by the tests: NaN in either argument gives NaN; atan2(±0.0, +x) is ±0.0; atan2(±0.0, -x) is ±pi; both infinite gives the diagonal (±pi/4, ±3pi/4).

Accuracy: ≤ 2 ulp over the reference set. Total: no error row, no traps.

float.atan2(0.0, 1.0) == 0.0
float.atan2(1.0, 1.0) == 0.7853981633974483
float.atan2(1.0, 0.0) == 1.5707963267948966
float.atan2(0.0, -1.0) == 3.141592653589793
float.atan2(-1.0, -1.0) == -2.356194490192345

powf

fn · Source line 1210

pub fn powf(b: f64, e: f64) -> f64

b raised to the real power e, as exp(e * ln b) with the sign and the special cases handled first. A negative base is legal only for an integral exponent (the real power is undefined otherwise) and answers NaN if the exponent is not integral.

The IEEE special cases this follows: powf(x, 0.0) is 1.0 for every x, NaN included; powf(1.0, y) is 1.0 for every y; powf(0.0, y) is 0.0 for positive y and +inf for negative y.

Accuracy: ≤ 4 ulp over the reference set. An integral exponent of magnitude at most 4096 takes the binary-exponentiation path and is exact wherever the answer is (powf(10.0, 3.0) is 1000.0, powf(2.0, 100.0) is the exact power); every other exponent goes through exp(e * ln b), whose error is ln b's multiplied by e — so the bound degrades with |e·ln b| and the docs say so rather than pretending otherwise. Total: no error row, no traps.

float.powf(2.0, 10.0) == 1024.0
float.powf(4.0, 0.5) == 2.0
float.powf(2.0, 0.0) == 1.0
float.powf(-2.0, 3.0) == -8.0
float.is_nan(float.powf(-2.0, 0.5)) == true

to_bits

fn · Source line 1347

pub fn to_bits(x: f64) -> int

The IEEE-754 binary64 encoding of x, read as a signed int: the sign bit lands in the sign of the result, so a negative float has a negative encoding and from_bits inverts it exactly. This is the exactness currency — two floats are the same value exactly when their bits agree, which == cannot say for NaN and says wrongly for -0.0.

Every NaN answers one canonical encoding (0x7FF8000000000000 read as an int): NaN payloads and sign are not observable at these pins, the same approximation std.cmp.total_cmp documents. Total: no error row, no traps.

let zero: int = 0
float.to_bits(0.0) == 0
float.to_bits(1.0) == 4607182418800017408
float.to_bits(2.0) == 4611686018427387904
float.to_bits(-2.0) == zero - 4611686018427387904
float.to_bits(-0.0) < 0

from_bits

fn · Source line 1432

pub fn from_bits(b: int) -> f64

The f64 whose IEEE-754 binary64 encoding, read as a signed int, is b — the inverse of to_bits on every finite value and on the infinities. Every NaN encoding answers nan(), matching to_bits's one canonical NaN.

Total: no error row, no traps — every int is some float's encoding.

let zero: int = 0
float.from_bits(0) == 0.0
float.from_bits(4607182418800017408) == 1.0
float.from_bits(zero - 4611686018427387904) == -2.0
float.from_bits(float.to_bits(0.125)) == 0.125
float.from_bits(9218868437227405312) == float.inf()

Constants

No public declarations in this section.

Errors and row tags

No public declarations in this section.