TorchLean API

FloatLib.Numerics.Quantization.Deterministic.Rational

Deterministic nearest-even rational rounding #

This is the exact Rat-to-Int specialization of the shared quotient-rounding decision. It uses integer numerator and denominator arithmetic throughout; no floating approximation is introduced while deciding a tie.

The proofs cover both signs, the half-unit error bound, fixed points, and exact half steps. Keeping those facts beside the executable definition gives fixed-point and mixed-precision code one rounding primitive with a stable, reusable contract.

@[inline]

Round an exact rational to the nearest integer, breaking halfway cases toward the even result.

Instances For
    theorem FloatLib.Numerics.roundQuotientEven_error_le_half (numerator denominator : ) (hdenominator : denominator 0) :
    |(roundQuotientEven numerator denominator) - numerator / denominator| 1 / 2

    Nearest-even quotient rounding differs from the exact nonnegative rational by at most one half.

    @[simp]
    theorem FloatLib.Numerics.roundRatEven_intCast (value : ) :
    roundRatEven value = value

    Nearest-even rational rounding fixes every integer exactly.

    Signed rational rounding has the same absolute error as rounding its magnitude.

    Nearest-even rational rounding differs from its exact input by at most one half.

    theorem FloatLib.Numerics.roundRatEven_eq_of_error_lt_half (x : ) (n : ) (h : |n - x| < 1 / 2) :

    A candidate strictly within half a unit is the unique rounded integer.

    The delivered integer is at least as close as every competing integer.

    theorem FloatLib.Numerics.roundQuotientEven_even_of_tie (numerator denominator : ) (htie : 2 * (numerator % denominator) = denominator) :
    roundQuotientEven numerator denominator % 2 = 0

    An exact quotient midpoint rounds to an even natural number.

    theorem FloatLib.Numerics.roundQuotientEven_error_lt_half (numerator denominator : ) (hd : denominator 0) (ht : 2 * (numerator % denominator) denominator) :
    |(roundQuotientEven numerator denominator) - numerator / denominator| < 1 / 2

    Away from a quotient midpoint the error is strictly less than half a unit.

    An exact quotient midpoint rounds to an even integer, regardless of its sign.

    Half-unit error can occur only when the delivered integer is even, for either sign.

    theorem FloatLib.Numerics.roundRatEven_half_step (n : ) :
    roundRatEven (n + 1 / 2) = if n % 2 = 0 then n else n + 1

    Every half-integer rounds to its even neighbor, including negative inputs.

    The shared quotient algorithm agrees with the floor/ceiling nearest-even decision.