TorchLean API

FloatLib.Floats.Formats.Flocq.Theory.Core

FloatRep core (Flocq-style rounded arithmetic) #

The rounded-real model separates representation, value, and format:

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 binary semantics (NaN/Inf/signed zero), see FloatLib/Floats/Formats/BinaryInterchange/.

References #

A radix-$\beta$ floating-point representation with integer mantissa and exponent.

  • mantissa :

    Integer mantissa m.

  • exponent :

    Integer exponent e.

Instances For

    Structural zero test (mantissa is exactly 0).

    Instances For

      Sign of the mantissa (matches the sign of toReal 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.

          theorem FloatLib.Floats.Formats.Flocq.bpow.add_exp (β : Numerics.Radix) (e1 e2 : ) :
          bpow β (e1 + e2) = bpow β e1 * bpow β e2

          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}$.

          theorem FloatLib.Floats.Formats.Flocq.bpow.sub_exp (β : Numerics.Radix) (e₁ e₂ : ) :
          bpow β (e₁ - e₂) = bpow β e₁ / bpow β e₂

          Exponent subtraction corresponds to division of radix powers.

          Interpret a FloatRep as the real number $m\beta^e$.

          Instances For
            @[simp]

            toReal 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.

              • flocq_valid (k : ) : (fexp k < kfexp (k + 1) k) (k fexp kfexp (fexp k + 1) fexp k lfexp k, fexp l = fexp k)

                Exponent compatibility at successive magnitudes and below a negligible exponent.

              Instances

                Exponent-selection functions that preserve order.

                • monotone (k1 k2 : ) : k1 k2fexp k1 fexp k2

                  Increasing the magnitude cannot decrease the selected exponent.

                Instances

                  Optional local growth bound used by selected numerical estimates.

                  • boundedGrowth (k : ) : |fexp (k + 1) - fexp k| 1

                    Successive magnitudes select exponents differing by at most one.

                  Instances

                    A witness that the format has a lower exponent region, in Flocq's sense.

                    Instances For

                      Select a witness $n\le\mathtt{fexp}(n)$ when the format has one. Unbounded formats such as FLX return none; lower-bounded formats such as FLT return some n.

                      Instances For

                        A selected negligible exponent satisfies $n\le\mathtt{fexp}(n)$.

                        A format has no selected negligible exponent exactly when no negligible exponent exists.

                        noncomputable def FloatLib.Floats.Formats.Flocq.cexp (β : Numerics.Radix) (fexp : ) [ValidExp fexp] (x : ) :

                        Canonical exponent (cexp in Flocq terminology).

                        Apply fexp to the input's magnitude; at zero this gives fexp 0. The ValidExp instance restricts the API to valid exponent functions, although evaluating this expression needs only fexp.

                        Instances For

                          A float representation is canonical when its stored exponent is the exponent selected for its value.

                          Instances For
                            noncomputable def FloatLib.Floats.Formats.Flocq.scaledMantissa (β : Numerics.Radix) (fexp : ) [ValidExp fexp] (x : ) :

                            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
                                noncomputable def FloatLib.Floats.Formats.Flocq.ulp (β : Numerics.Radix) (fexp : ) [ValidExp fexp] (x : ) :

                                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$.

                                At zero, a negligible exponent determines the spacing when one exists; otherwise the ULP is zero.

                                Instances For
                                  theorem FloatLib.Floats.Formats.Flocq.ulp.nonneg (β : Numerics.Radix) (fexp : ) [ValidExp fexp] (x : ) :
                                  0 ulp β fexp x

                                  ulp is always nonnegative.

                                  Informally: an ulp is a step size on a real grid, so it cannot be negative.

                                  theorem FloatLib.Floats.Formats.Flocq.ulp.pos_of_ne_zero (β : Numerics.Radix) (fexp : ) [ValidExp fexp] (x : ) (hx : x 0) :
                                  0 < ulp β fexp x

                                  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.

                                  @[simp]
                                  theorem FloatLib.Floats.Formats.Flocq.ulp.zero (β : Numerics.Radix) (fexp : ) [ValidExp fexp] :
                                  ulp β fexp 0 = match negligibleExp fexp with | some n => bpow β (fexp n) | none => 0

                                  The ULP at zero is determined by the format's negligible exponent, when one exists.

                                  @[simp]
                                  theorem FloatLib.Floats.Formats.Flocq.ulp.of_ne_zero (β : Numerics.Radix) (fexp : ) [ValidExp fexp] (x : ) (hx : x 0) :
                                  ulp β fexp x = bpow β (cexp β fexp x)

                                  Away from zero, ulp is the base grid step $\beta^{\operatorname{cexp}(x)}$.