TorchLean API

NN.Floats.Calc.Round

Rounding from a Certified Bracket #

The effective calculation layer identifies a unit interval [m, m + 1) and a location inside it. This file turns that finite location data into the integer selected by directed or nearest rounding and proves agreement with the rounded-real definitions.

@[reducible, inline]

Unit-interval specialization of NeuralInbetween.

Instances For

    Canonical location obtained from the floor bracket of a real value.

    Instances For

      Every real value satisfies its canonical floor bracket.

      Increment an integer when the supplied decision is true.

      Instances For

        A conditional increment always selects one of the two bracket endpoints.

        Upward rounding increments exactly when the location is inexact.

        Instances For

          Nearest rounding increments above the midpoint and delegates exact ties to chooseUp.

          Instances For

            Tie decision used by nearest-even rounding: increment exactly when the lower integer is odd.

            Instances For

              Choice-based nearest rounding with the parity decision is TorchLean's nearest-even mode.

              theorem TorchLean.Floats.neuralInbetweenInt_floor {m : } {x : } {location : NeuralLocation} (hl : NeuralInbetweenInt m x location) :

              A certified unit bracket determines floor exactly.

              A certified unit bracket determines ceiling from exactness alone.

              theorem TorchLean.Floats.neuralInbetweenInt_nearestChoice (chooseUp : Bool) {m : } {x : } {location : NeuralLocation} (hl : NeuralInbetweenInt m x location) :

              A certified unit bracket computes arbitrary-tie nearest rounding.

              A certified unit bracket computes nearest-even rounding.

              Mantissa truncation #

              Mantissa, exponent, and location carried by an effective rounding calculation.

              • mantissa :

                Lower-endpoint mantissa.

              • exponent :

                Shared radix exponent.

              • location : NeuralLocation

                Input location within the represented unit interval.

              Instances For
                @[reducible, inline]

                Real interval and location denoted by a truncation state.

                Instances For

                  A positive value bracketed by a truncation state forces a nonnegative lower mantissa.

                  theorem TorchLean.Floats.neuralCexp_eq_fexp_digits_of_brackets {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (state : NeuralTruncationState) {x : } (hx : 0 < x) (hl : NeuralTruncationState.Brackets β state x) (hexp : state.exponent neuralCexp β fexp x state.exponent fexp ((neuralDigits β state.mantissa) + state.exponent)) :
                  neuralCexp β fexp x = fexp ((neuralDigits β state.mantissa) + state.exponent)

                  A positive bracket determines the canonical exponent from the lower mantissa's digit count. This is the representation-level counterpart of Flocq's cexp_inbetween_float.

                  theorem TorchLean.Floats.NeuralTruncationState.scaledBrackets_of_exponent_eq {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (state : NeuralTruncationState) {x : } (hl : Brackets β state x) (hexponent : state.exponent = neuralCexp β fexp x) :

                  A bracket stored at the canonical exponent is a unit bracket for the scaled mantissa.

                  Discard shift low radix digits and transfer their information into the refined location. Positive shifts are the intended use; correctness theorems state that premise explicitly.

                  Instances For
                    theorem TorchLean.Floats.neuralIntPower_one_lt (β : NeuralRadix) {shift : } (hshift : 0 < shift) :
                    1 < neuralIntPower β shift

                    A positive shift produces a radix power strictly larger than one.

                    theorem TorchLean.Floats.neuralTruncateAux_remainder_bounds (β : NeuralRadix) (state : NeuralTruncationState) {shift : } (hshift : 0 < shift) :
                    have power := neuralIntPower β shift; 0 state.mantissa % power state.mantissa % power < power

                    The truncated remainder is a valid cell index in the discarded radix block.

                    theorem TorchLean.Floats.neuralTruncateAux_mantissa_decomposition (β : NeuralRadix) (state : NeuralTruncationState) {shift : } (hshift : 0 < shift) :
                    state.mantissa = state.mantissa % neuralIntPower β shift + neuralIntPower β shift * (neuralTruncateAux β state shift).mantissa

                    Mantissa reconstruction after one truncation step.

                    theorem TorchLean.Floats.neuralTruncateAux_brackets (β : NeuralRadix) (state : NeuralTruncationState) {shift : } (hshift : 0 < shift) {x : } (hl : NeuralTruncationState.Brackets β state x) :

                    One positive truncation step preserves the represented real bracket and its refined location.

                    Number of low radix digits discarded to reach the exponent selected by fexp.

                    Instances For

                      Truncate only when the target exponent lies strictly above the stored exponent.

                      Instances For

                        Format-driven truncation preserves the represented real bracket.

                        theorem TorchLean.Floats.neuralTruncate_brackets_and_exponent {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (state : NeuralTruncationState) {x : } (hx : 0 < x) (hl : NeuralTruncationState.Brackets β state x) (hexp : state.exponent neuralCexp β fexp x state.exponent fexp ((neuralDigits β state.mantissa) + state.exponent)) :

                        For a positive input with sufficient initial precision, truncation both preserves the bracket and selects the canonical format exponent.

                        Nearest-even result selected from a format-truncated bracket.

                        Instances For
                          @[simp]
                          theorem TorchLean.Floats.neuralTruncate_zero_mantissa (β : NeuralRadix) (fexp : ) (exponent : ) (location : NeuralLocation) :
                          (neuralTruncate β fexp { mantissa := 0, exponent := exponent, location := location }).mantissa = 0

                          Truncation never changes a zero mantissa into a nonzero one.

                          noncomputable def TorchLean.Floats.neuralRoundedFloat {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (rnd : ) (x : ) :

                          Canonical mantissa/exponent representation produced by a rounding mode.

                          Instances For
                            theorem TorchLean.Floats.neuralRound_eq_toReal_of_scaled_round_eq {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (rnd : ) (x : ) (mantissa : ) (hmantissa : rnd (neuralScaledMantissa β fexp x) = mantissa) :
                            neuralRound rnd x = neuralToReal { mantissa := mantissa, exponent := neuralCexp β fexp x }

                            If a finite decision procedure computes the rounded scaled mantissa, neuralRound is exactly the real value of the corresponding mantissa/exponent pair.

                            theorem TorchLean.Floats.neuralRound_floor_of_scaledBracket {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (x : ) {m : } {location : NeuralLocation} (hl : NeuralInbetweenInt m (neuralScaledMantissa β fexp x) location) :
                            neuralRound neuralFloorRound x = neuralToReal { mantissa := m, exponent := neuralCexp β fexp x }

                            A scaled-mantissa bracket computes format-level downward rounding.

                            theorem TorchLean.Floats.neuralRound_ceil_of_scaledBracket {β : NeuralRadix} {fexp : } [NeuralValidExp fexp] (x : ) {m : } {location : NeuralLocation} (hl : NeuralInbetweenInt m (neuralScaledMantissa β fexp x) location) :

                            A scaled-mantissa bracket computes format-level upward rounding.

                            A scaled-mantissa bracket computes format-level nearest-even rounding.

                            Canonical mantissa selected by effective nearest-even rounding.

                            Instances For

                              Every nearest-even format rounding has a canonical computed mantissa/exponent representation.

                              Nearest-even selection after canonical truncation agrees with generic rounded-real semantics.