'Is conversion from real floating to integer requires that the value is truncated toward zero to facilitate porting of code from Fortran to C?
C11, 6.3.1.4 Real floating and integer:
When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero).
What is the rationale for "the value is truncated toward zero"? Why, for example, not "rounds to the nearest integral value, with halfway cases rounded to even" (IEEE 754, convertToIntegerTiesToEven(x)
)?
From document "Rationale for International Standard -- Programming Languages -- C", Revision 5.10, April-2003:
6.3.1.5 Real floating types (emphasis added):
Since truncation-toward-zero is the appropriate setting for C in the former case, it would be expensive to require such implementations to round to float.
6.5.5 Multiplicative operators (emphasis added):
In C89, division of integers involving negative operands could round upward or downward in an implementation-defined manner; the intent was to avoid incurring overhead in run-time code to 10 check for special cases and enforce specific behavior. In Fortran, however, the result will always truncate toward zero, and the overhead seems to be acceptable to the numeric programming community. Therefore, C99 now requires similar behavior, which should facilitate porting of code from Fortran to C.
Solution 1:[1]
Your question is answered in the paragraph before the one you quoted. 6.3.1.4 says “Although K&R permitted negative floating values to truncate away from zero, no C89 Committee member knew of an implementation that functioned in such a manner.” I.e., every known C implementation truncated floating-point values toward zero, so that was made the standard (since it ensures future uniformity).
K&R first edition says, on page 42, “… float
to int
causes truncation of any fractional part,” and, on page 184 “… Conversions of floating values to integral type tend to be rather machine-dependent; in particular the direction of truncation of negative numbers varies from machine to machine…”
So there you have it. K&R provided for “truncation” either toward zero or, effectively, toward ??. The C committee found nobody doing the latter, so they adopted the former. If you want to know more, you will have to ask Brian Kernighan or look for it in their writings.
Solution 2:[2]
The simplest is usually the best one.
There were many rounding modes and various benefits to them in 1970s as well as today. C sought a compromise based on existing practice. As rounding modes were not that uniform in the industry then (IMO, they still aren't), the simplest, truncation, was spec'd
IEEE 754 specifications (1980s) came after C (1970s). IEEE 754 offers guidance today, but was not so much universally adopted in early C days. Various manufacturers had their own preferred solutions.
I suspect @Eric's idea "ensures future uniformity" was a key factor too.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eric Postpischil |
Solution 2 |