floating-point => integral conversions and rounding
David G. Hough at validgh
uunet!validgh.com!dgh
Tue Dec 21 11:07:06 PST 1993
> Much to my surprize, I found that the ISO C standard and the IEEE 754
> standard are in direct conflict regarding what is supposed to happen for
> such conversions.
>
> The C standard says that "...the fractional part is discarded." (I think
> that may be just a bit ambiguous in the case of negative numbers... but
> we'll just pass on that for the moment.)
>
> Anyway, the IEEE 754 standard says that the default rounding which must
> apply to such conversions is "round to nearest".
There is no problem here, since the IEEE standard doesn't specify the
language binding of its features.
IEEE 754 specifies operations for conversion from floating-point formats
to integer formats. These operations are subject to the floating-point
rounding direction modes, with round to nearest by default.
Fortran and C and many other language standards specify operations for
conversion from floating-point formats to integer formats. These
operations round toward zero.
Nothing identifies these IEEE 754 operations with those Fortran/C/...
operations since they aren't the same. For instance, SunPro's compilers
include functions like irint() which converts double to int according
to the IEEE standard. (int) converts double to int rounding toward zero
in accordance with C.
The IEEE spec may have been a mistake in this area; a better choice would
have been to specify conversions to integral values in integer or floating
formats with fixed rounding modes, and without the inexact exception.
But regardless of one's opinion about that, there's no conflict with C or
Fortran.
More information about the Numeric-interest
mailing list