Integral-rounding contracts #
The unbounded dyadic-to-integer kernel is related to the usual nearest-even, nearest-away, floor, ceiling, truncation, and away-from-zero semantics. The floating-point wrapper then records exactly when fractional bits were discarded and exposes the exceptional branches.
For IEEE encodings with fmt.fracWidth ≤ fmt.maxNormalExponent, the selected integer is exactly
representable. This condition holds for the IEEE interchange formats. On finite inputs,
roundToIntegral then denotes the integer selected by the rounding direction and reports no
overflow. Truncation toward zero needs no such range
condition: its result cannot exceed the input magnitude. Nearest-away rounding is
roundToIntegralTiesToAway of IEEE 754-2019, Section 5.9, and it is Flocq's nearestChoice with
the tie choice nearestAwayChoice.
References #
- IEEE 754-2019, Sections 4.3.2 and 5.9 (directed and integral rounding). https://doi.org/10.1109/IEEESTD.2019.8766229
Exact zero rounds to integer zero in every direction.
Nearest-even integer rounding has its usual mathematical meaning, independently of the stored dyadic exponent or magnitude.
Rounding toward positive infinity computes the mathematical ceiling.
Rounding toward negative infinity computes the mathematical floor.
Rounding toward zero truncates the exact dyadic: nonnegative values use floor and negative values use ceiling.
Nearest-away integer rounding is Flocq's nearest rounding with ties away from zero, independently of the stored dyadic exponent or magnitude.
Rounding away from zero takes the ceiling of a nonnegative exact dyadic and the floor of a negative one.
A dyadic with a nonnegative exponent is already an integer.
Dyadic zero is integral independently of its stored sign and exponent.
An exact dyadic is integral exactly when no negative binary exponent leaves a fractional bit.
For exponent -(shift + 1), this says that 2^(shift + 1) divides the significand. A
nonnegative exponent is always integral.
Integral rounding with status := true delivers the value of integral rounding.
On finite input, roundToIntegral raises inexact exactly when a negative binary
exponent leaves at least one fractional bit.
roundToIntegral returns either infinity unchanged, with clear status.
roundToIntegral quiets a NaN and raises invalid exactly when it was signaling.
Representability of the rounded integer #
Every rounding direction lands at or above the floor of the exact value.
Every rounding direction lands at or below the ceiling of the exact value.
If the significand is below 2^(fracWidth + 1) and the binary exponent is negative, the rounded
integer has magnitude at most 2^fracWidth.
An integer placed at exponent zero denotes itself.
On finite input, roundToIntegral rounds the exact dyadic to an unbounded integer,
then encodes that integer in the selected direction. The status records discarded fractional bits
and the encoder's overflow classification, and the ternary value compares the delivered value with
the input.
On a finite input of an IEEE format whose exponent range reaches the precision, the selected
integer is represented exactly and reports no overflow. Fractional input can still set inexact.
The range condition holds for IEEE 754's standard binary interchange formats.
Truncation toward zero cannot increase the magnitude of an exact dyadic.
Truncating a finite IEEE value toward zero is finite, for any exponent range.
Truncation toward zero computes floor or ceiling without an exponent-range restriction.
Truncation toward zero of a finite IEEE value never signals overflow.
Integral rounding of a finite IEEE value never overflows when
fmt.fracWidth ≤ fmt.maxNormalExponent.
Integral rounding of a finite IEEE value is finite when the exponent range reaches the precision.
The real value of a finite integral rounding is the integer selected by roundDyadicToInt.
The result of integral rounding is an integer.
Rounding toward negative infinity computes the floor of the real value.
Rounding toward positive infinity computes the ceiling of the real value.
Rounding toward zero truncates the real value: floor when nonnegative, ceiling otherwise.
Rounding to nearest with ties away from zero computes the nearest integer and resolves a tie toward
the integer of larger magnitude, roundToIntegralTiesToAway of IEEE 754-2019. This is Flocq's
nearestChoice nearestAwayChoice.
Rounding away from zero computes the ceiling of a nonnegative value and the floor of a negative
one, as MPFR's mpfr_rint does under MPFR_RNDA.
Rounding to nearest computes the nearest integer with ties to even, nearestEven from the Flocq
theory. Mathlib's round resolves ties toward positive infinity.