TorchLean API

FloatLib.Floats.Formats.BinaryInterchange.Rounding.Proof

Correctness of nearest-even real rounding #

For conventional IEEE descriptors, Model.roundDyadic delegates normalization to Lean's generic logical float model. This module relates that executable result to the independent Flocq-style rounded-real semantics in FloatLib.Floats.Formats.Flocq.FloatRep. Exponent and fraction widths remain symbolic. The grid membership and preservation results also cover custom biases and encoding policies; the executable rounding refinement requires fmt.isIEEE = true.

The rounded-real model has gradual underflow but no infinities. Consequently refinement theorems carry an explicit hypothesis that the executable result is finite; an overflowing IEEE result cannot equal a real-valued rounding function.

References #

Precision and gradual-underflow grid using the descriptor’s declared exponent bias.

Instances For
    @[reducible, inline]

    Nearest-even rounding with the descriptor's precision, bias, and gradual underflow.

    The real grid has no upper exponent bound. Overflow and exceptional encodings belong to the executable operation; its equality to roundAt needs the corresponding refinement hypotheses.

    Instances For

      Nearest-even rounding on a binary format fixes zero.

      @[simp]

      Nearest-even rounding on a binary format commutes with negation.

      Nearest-even rounding is monotone on the exact real input.

      Every finite decoded word belongs to its descriptor's precision and gradual-underflow grid. The result includes custom biases, FNUZ, and finite-only encodings.

      @[simp]

      Rounding a finite decoded value back to its own format is an exact identity.

      A nonzero dyadic has the leading exponent predicted by its integer mantissa.

      Lean's model and the rounded-real format choose the same exponent for a nonzero dyadic.

      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.nearestEven_scaledMagnitude (mantissa : ) (exponent targetExponent : ) :
      Flocq.nearestEven (mantissa * Flocq.bpow Numerics.binaryRadix (exponent - targetExponent)) = Int.ofNat (roundMantissaAtExponentEven mantissa exponent targetExponent)

      Nearest-even rounding of a nonnegative dyadic agrees with executable shift-and-round.

      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.nearestEven_scaledDyadic (sign : Bool) (mantissa : ) (exponent targetExponent : ) :
      Flocq.nearestEven ((if sign = true then -1 else 1) * mantissa * Flocq.bpow Numerics.binaryRadix (exponent - targetExponent)) = if sign = true then -Int.ofNat (roundMantissaAtExponentEven mantissa exponent targetExponent) else Int.ofNat (roundMantissaAtExponentEven mantissa exponent targetExponent)

      Nearest-even rounding commutes with the sign stored in a dyadic value.

      The executable natural power of two has the same real value as the binary radix power.

      Rounded-real semantics of a nonzero dyadic at a conventional IEEE descriptor.

      The integer on the right is computed entirely by shifts and nearest-even rounding. The exponent is chosen by Lean's generic logical float model and independently characterized by fexpOf.

      Real value of a finished rounded mantissa on the subnormal grid.

      The mantissa may be zero, in which case the packed value is a signed zero, or reach 2 ^ fracWidth, in which case it is the least normal value; both have the stated real value.

      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.toReal_ofModel_finishRoundedMantissa_normal (fmt : FloatFormat) (hfmt : fmt.isIEEE = true) (sign : Float.Model.UnpackedFloat.Sign) (rounded : ) (k : ) (hlow : pow2 fmt.fracWidth rounded) (hhigh : rounded pow2 (fmt.fracWidth + 1)) (hk : fmt.ieeeMinNormalExponent k) (hfin : (ofModel fmt (finishRoundedMantissa fmt.toModel sign (rounded, k - fmt.fracWidth))).isFinite = true) :
      (ofModel fmt (finishRoundedMantissa fmt.toModel sign (rounded, k - fmt.fracWidth))).toReal = (if modelSignBit sign = true then -1 else 1) * (rounded * Flocq.bpow Numerics.binaryRadix (k - fmt.fracWidth))

      Real value of a finished normalized rounded mantissa with leading bit at unbiased exponent k.

      The mantissa lies in [2 ^ fracWidth, 2 ^ (fracWidth + 1)]; the upper endpoint is the carry that finishRoundedMantissa renormalizes to exponent k + 1. The finiteness hypothesis excludes IEEE overflow, which has no value in .

      Executable dyadic rounding refines the rounded-real semantics for conventional IEEE descriptors.

      The finiteness hypothesis excludes the one case that has no value in : IEEE overflow to infinity. Both the IEEE-descriptor condition and result finiteness are explicit hypotheses; custom widths are allowed when they satisfy that descriptor condition.