NeuralFloat core (Flocq-style rounded arithmetic) #
TorchLean frequently reasons about floating-point behavior using a classical "rounded arithmetic on $\mathbb{R}$" approach rather than a bit-level IEEE-754 model:
- represent a value with integer mantissa $m$ and exponent $e$,
- interpret it as the real number $m\beta^e$,
- describe the format via an exponent-selection function
fexp : ℤ → ℤand a rounding operator.
This decomposition is the same one used by the Coq library Flocq. It makes many theorems reusable across formats (fixed-point, unbounded floats, and lower-exponent-bounded floats) and aligns well with ULP-style error bounds from numerical analysis.
For executable, bit-level IEEE-754 semantics (NaN/Inf/signed zero), see NN/Floats/IEEEExec/.
Training annotations and named precision policies are deliberately kept out of this mathematical
core; see NN/Floats/NeuralFloat/Metadata.lean.
References #
- Flocq project (documentation + sources): https://flocq.gitlabpages.inria.fr/flocq/
- S. Boldo, G. Melquiond, “Flocq: a unified Coq library for proving floating-point algorithms correct” (ARITH 2011), DOI: 10.1109/ARITH.2011.40
- IEEE Standard for Floating-Point Arithmetic (IEEE 754-2019)
- N. J. Higham, Accuracy and Stability of Numerical Algorithms, 2nd ed., SIAM, 2002
Radix (base) for "floating-point-like" representations.
In practice we almost always use base $2$ (binaryRadix) because that's what hardware implements,
but keeping the base explicit helps make the model match the literature (and it keeps some proofs
parametric in the radix).
- base : ℕ
Radix base (e.g.
2for binary). Validity condition: the base is at least 2.
Instances For
Decimal radix ($\beta=10$, useful for compact examples and exact decimal inputs).
Instances For
Coerce the radix base to $\mathbb{R}$ (used by bpow and logarithms).
Instances For
The radix base is positive when viewed as a real number.
The radix base is nonzero when viewed as a real number.
The radix base is strictly greater than $1$ (for a valid radix, $2\le\mathtt{base}$).
A radix-$\beta$ floating-point representation with integer mantissa and exponent.
Instances For
Structural zero test (mantissa is exactly 0).
Instances For
Sign of the mantissa (matches the sign of neuralToReal since $\beta^e>0$).
Instances For
Base power: $\beta^e$ as a real number.
This is Flocq's bpow concept: the scaling factor used to interpret mantissa/exponent pairs.
Instances For
Base powers are positive: $\beta^e>0$ for any exponent $e$.
Base powers are nonnegative: $\beta^e\ge0$ for any exponent $e$.
Base powers are never zero.
Exponent addition law: $\beta^{e_1+e_2}=\beta^{e_1}\beta^{e_2}$.
Negating the exponent inverts the base power: $\beta^{-e}=(\beta^e)^{-1}$.
Exponent subtraction corresponds to division of radix powers.
Interpret a NeuralFloat as the real number $m\beta^e$.
Instances For
neuralToReal is zero iff the mantissa is zero (since $\beta^e\ne0$).
Magnitude in base $\beta$ of a real number.
This matches the usual definition
$\operatorname{mag}(x)=\lfloor\log_\beta|x|\rfloor+1$ for $x\ne0$, and $0$ for $x=0$.
It is the bridge between a real input $x$ and the exponent-selection function fexp.
Instances For
Validity predicate for exponent-selection functions.
This is the exponent-validity condition used by Flocq. Properties that are not consequences of validity, such as monotonicity, are separate classes so generic results do not acquire unnecessary hypotheses.
Instances
A selected negligible exponent satisfies $n\le\mathtt{fexp}(n)$.
A format has no selected negligible exponent exactly when no negligible exponent exists.
Canonical exponent (cexp in Flocq terminology).
Given a nonzero x, we first compute its magnitude mag(x) and then apply fexp to pick the
exponent used for scaling/rounding.
Instances For
A float representation is canonical when its stored exponent is the exponent selected for its value.
Instances For
Scaled mantissa $x\beta^{-\operatorname{cexp}(x)}$.
Intuitively: rescale x so that rounding “happens around exponent 0”, which is where rnd acts.
Instances For
Generic format predicate (Flocq-style).
This says that $x$ is exactly representable in the format picked out by $\beta$ and fexp.
One way to read it is: the scaled mantissa is an integer (so there is no rounding error).
Instances For
Unit in the last place (ULP) associated with $x$ and the format selected by fexp.
This is the scale of the “one ulp” step at the exponent selected by cexp. For round-to-nearest,
many standard bounds have the shape
$|\operatorname{round}(x)-x|\le\operatorname{ulp}(x)/2$.
An ULP is a property of the format and does not depend on runtime provenance annotations.
Instances For
neural_ulp is always nonnegative.
Informally: an ulp is a step size on a real grid, so it cannot be negative.
neural_ulp is strictly positive away from zero.
If $x\ne0$, the exponent selection $\operatorname{cexp}(x)$ picks a power of $\beta$, which is strictly positive.
The ULP at zero is determined by the format's negligible exponent, when one exists.
Away from zero, neuralUlp is the base grid step $\beta^{\operatorname{cexp}(x)}$.