TorchLean API

FloatLib.Floats.Formats.DecimalInterchange.Conversion.Format.Proof

Decimal format conversion guarantees #

The numerical statements concern the returned datum for every destination descriptor. Nonoverflowing nearest conversion has at most half a decimal grid unit of error; directed conversion encloses the input on the required side. Representable finite inputs retain their exact value and raise no exception.

theorem FloatLib.Floats.Formats.DecimalInterchange.Conversion.project_signed_magnitude (f : Format) (mode : RoundingMode) (s : Bool) (x : ) (hx : 0 x) (preferred : ) :
project f mode (if s = true then -x else x) preferred s = projectMagnitude f mode s x preferred

Projection preserves an explicit sign when the input is presented as a magnitude.

Finite conversion uses the supplied sign and the exact nonnegative decimal magnitude.

Every conversion produces a valid destination datum, even from an unrestricted datum.

@[simp]

Infinity retains its sign and raises no exception.

@[simp]
theorem FloatLib.Floats.Formats.DecimalInterchange.Conversion.convertFormat_nan (f : Format) (mode : RoundingMode) (s signaling : Bool) (p : ) :
convertFormat f mode (Datum.nan s signaling p) = { value := Datum.nan s false (if p < f.payloadBound then p else 0), status := { invalid := signaling } }

NaN conversion preserves a fitting payload, quiets the NaN, and reports its signaling bit.

@[simp]

Signed zero keeps its sign and clamps its source quantum to the destination interval.

A finite input already representable in the destination retains its exact numerical value.

Changing destination width or cohort cannot raise a flag on an exactly representable input.

theorem FloatLib.Floats.Formats.DecimalInterchange.Conversion.convertFormat_exact_of_representable (f : Format) (mode : RoundingMode) (s : Bool) (c d : ) (q r : ) (hd : Datum.Valid f (Datum.finite s d r)) (hvalue : c * 10 ^ q = d * 10 ^ r) :

A source cohort need not itself fit the destination: any valid destination representation of the same magnitude makes the conversion exact.

theorem FloatLib.Floats.Formats.DecimalInterchange.Conversion.convertFormat_exact_status_of_representable (f : Format) (mode : RoundingMode) (s : Bool) (c d : ) (q r : ) (hd : Datum.Valid f (Datum.finite s d r)) (hvalue : c * 10 ^ q = d * 10 ^ r) :

A representable exact value raises no flags even if its source coefficient or quantum must change to fit the destination.

theorem FloatLib.Floats.Formats.DecimalInterchange.Conversion.convertFormat_quantum_closest (f : Format) (mode : RoundingMode) (s : Bool) (c : ) (q : ) (h : Datum.Valid f (Datum.finite s c q)) (d : ) (r : ) (hout : (convertFormat f mode (Datum.finite s c q)).value = Datum.finite s d r) (e : ) (t : ) (he : Datum.Valid f (Datum.finite s e t)) (hvalue : c * 10 ^ q = e * 10 ^ t) :
|q - r| |q - t|

Exact conversion selects the quantum closest to the source quantum over the entire cohort.

theorem FloatLib.Floats.Formats.DecimalInterchange.Conversion.convertFormat_quantum_closest_of_representable (f : Format) (mode : RoundingMode) (s : Bool) (c d : ) (q r : ) (hd : Datum.Valid f (Datum.finite s d r)) (hvalue : c * 10 ^ q = d * 10 ^ r) (e : ) (t : ) (hout : (convertFormat f mode (Datum.finite s c q)).value = Datum.finite s e t) (a : ) (b : ) (ha : Datum.Valid f (Datum.finite s a b)) (heq : c * 10 ^ q = a * 10 ^ b) :
|q - t| |q - b|

Across different source and destination widths, exact conversion selects a destination cohort member whose quantum is closest to the source quantum.

Both nearest directions bound the actual conversion error by half the selected grid unit.

theorem FloatLib.Floats.Formats.DecimalInterchange.Conversion.convertFormat_error_lt_one (f : Format) (mode : RoundingMode) (x : Datum) (a : ) (hx : x.toRat? = some a) (hfinite : (convertFormat f mode x).status.overflow = false) :
∃ (value : ), (convertFormat f mode x).value.toRat? = some value |value - a| < 10 ^ roundingQuantum f |a|

All five directions keep a nonoverflowing conversion within one selected grid unit.

Upward conversion encloses the input from above whenever it does not overflow.

Downward conversion encloses the input from below whenever it does not overflow.

Finite nonoverflowing conversion raises inexact exactly when the numerical value changes.

Decimal underflow uses the exact input magnitude and requires inexactness.