[Haskell-cafe] Rounding
Richard A. O'Keefe
ok at cs.otago.ac.nz
Fri Jun 5 03:48:52 UTC 2015
On 2/06/2015, at 7:30 pm, Magnus Therning <magnus at therning.org> wrote:
> This is pure conjecture, but reading about "round half to even" on
> Wikipedia (<http://en.wikipedia.org/wiki/Rounding#Round_half_to_even>)
> shows that it has the most aliases of all tie-breaking strategies, it
> is also the rounding used in IEEE 754.
I'm not really interested in "pure conjecture" at this point.
For what it's worth, in my copy of IEEE 754 it says
<quote>
5.4. Conversion Between Floating-Point and Integer Formats
It shall be possible to convert between all supported floating-point formats and all supported integer formats. Conversion to integer shall be effected by rounding as specified in Section 4. Conversions between floating-point integers and integer formats shall be exact unless an exception arises as specified in 7.1.
5.5. Round Floating-Point Number to Integer Value
It shall be possible to round a floating-point number to an integral valued floating-point number in the same format. The rounding shall be as specified in Section 4, with the understanding that when rounding to nearest, if the difference between the unrounded operand and the rounded result is exactly one half, the rounded result is even.
</quote>
Section 4 specifies round-to-nearest-breaking-ties-to-even,
round-to-plus-infinity, round-to-minus-infinity, and round-to-zero.
Reading closely, we see that
5.4: it must be *possible* to convert between any size of float
and any size of integer using any one of the four rounding rules
at the user's choice. So the *effects* of RealFrac's
{floor, ceiling, truncate, round} are required, but not those
specific names.
5.5: it must be possible to convert any size of float to integer
in the same format. This is subject to the current rounding mode.
C's rint{,l,f} function does this.
If there is anything in Haskell 2010 that meets the requirements
of section 5.5, I have been unable to find it.
Since IEEE 754 conformance is not a goal of Haskell (there is,
for example, no way in Haskell 2010 to set the rounding
direction), there has to be some *other* reason than IEEE
conformance to explain the adoption of the break-ties-to-even
rule for rounding.
In any case, good support for IEEE 754 arithmetic since C99
hasn't stopped C providing rounding functions with the
traditional semantics *as well as* ones with IEEE semantics.
More information about the Haskell-Cafe
mailing list