TorchLean API

FloatLib.Floats.Formats.DecimalInterchange.Rounding.Proof

Numerical guarantees for exact coefficient rounding #

The computed coefficient fixes nonnegative integers, brackets the exact magnitude, satisfies directed inequalities, and has at most half a grid unit of error in either nearest mode.

@[simp]

Every nonnegative integer is fixed in every rounding direction.

The rounded coefficient is one of the two adjacent integer grid points.

A directed or nearest rounding changes a nonnegative value by less than one grid unit.

Both nearest modes have an error of at most half an integer grid unit.

Rounding toward zero does not increase magnitude.

Upward rounding is an upper enclosure for either sign.

Downward rounding is a lower enclosure for either sign.

At an exact midpoint, nearest-even chooses the even adjacent coefficient.

Nearest-away increases magnitude at every exact midpoint, for either sign.

theorem FloatLib.Floats.Formats.DecimalInterchange.RoundingMode.roundAt_error_lt_one (mode : RoundingMode) (s : Bool) {x : } (hx : 0 x) (q : ) :
|(mode.roundAt s x q) * 10 ^ q - x| < 10 ^ q

All five modes have strictly less than one decimal grid unit of error.

theorem FloatLib.Floats.Formats.DecimalInterchange.RoundingMode.roundAt_error_le_half (mode : RoundingMode) (hm : mode = nearestEven mode = nearestAway) (s : Bool) {x : } (hx : 0 x) (q : ) :
|(mode.roundAt s x q) * 10 ^ q - x| 10 ^ q / 2

The error bound scales by the actual decimal quantum, for any exponent.

theorem FloatLib.Floats.Formats.DecimalInterchange.RoundingMode.roundAt_exact (mode : RoundingMode) (s : Bool) (c : ) {q r : } (h : r q) :
mode.roundAt s (c * 10 ^ q) r = c * 10 ^ (q - r).toNat

A finite decimal remains exact on every finer decimal grid.

theorem FloatLib.Floats.Formats.DecimalInterchange.RoundingMode.roundAt_exact_value (mode : RoundingMode) (s : Bool) (c : ) {q r : } (h : r q) :
(mode.roundAt s (c * 10 ^ q) r) * 10 ^ r = c * 10 ^ q

Exact grid refinement preserves value in all five modes.

An integer upper bound on the magnitude remains an upper bound after any rounding.