views:

148

answers:

1

EDIT: I had made a mistake during the debugging session that lead me to ask this question. The differences I was seeing were in fact in printing a double and in parsing a double (strtod). Stephen's answer still covers my question very well even after this rectification, so I think I will leave the question alone in case it is useful to someone.

Some (most) C compilation platforms I have access to do not take the FPU rounding mode into account when

  • converting a 64-bit integer to double;
  • printing a double.

Nothing very exotic here: Mac OS X Leopard, various recent Linuxes and BSD variants, Windows.

On the other hand, Mac OS X Snow Leopard seems to take the rounding mode into account when doing these two things. Of course, having different behaviors annoys me no end.

Here are typical snippets for the two cases:

#if defined(__OpenBSD__) || defined(__NetBSD__) 
# include <ieeefp.h>
# define FE_UPWARD FP_RP
# define fesetround(RM) fpsetround(RM)
#else 
# include <fenv.h>
#endif

#include <float.h>
#include <math.h>

fesetround(FE_UPWARD);

...
double f;
long long b = 2000000001;
b = b*b;
f = b;

...
printf("%f\n", 0.1);

My questions are:

  1. Is there something non-ugly that I can do to normalize the behavior across all platforms? Some hidden setting to tell the platforms that take rounding mode into account not to or vice versa?
  2. Is one of the behaviors standard?
  3. What am I likely to encounter when the FPU rounding mode is not used? Round towards zero? Round to nearest? Please, tell me that there is only one alternative :)

Regarding 2. I found the place in the standard where it is said that floats converted to integers are always truncated (rounded towards zero) but I couldn't find anything for the integer -> float direction.

+2  A: 

If you have not set the rounding mode, it should be the IEEE-754 default mode, which is round-to-nearest.

For conversions from integer to float, the C standard says (§6.3.1.4):

When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.

So both behaviors conform to the C standard.

The C standard says (§F.5) that conversions between IEC60559 floating point formats and character sequences be correctly rounded as per the IEEE-754 standard. For non-IEC60559 formats, this is recommended, but not required. The 1985 IEE-754 standard says (clause 5.4):

Conversions shall be correctly rounded as specified in Section 4 for operands lying within the ranges specified in Table 3. Otherwise, for rounding to nearest, the error in the converted result shall not exceed by more than 0.47 units in the destination's least significant digit the error that is incurred by the rounding specifications of Section 4, provided that exponent over/underflow does not occur. In the directed rounding modes the error shall have the correct sign and shall not exceed 1.47 units in the last place.

What section (4) actually says is that the operation shall occur according to the prevailing rounding mode. I.e. if you change the rounding mode, IEEE-754 says that the result of float->string conversion should change accordingly. Ditto for integer->float conversions.

The 2008 revision of the IEEE-754 standard says (clause 4.3):

The rounding-direction attribute affects all computational operations that might be inexact. Inexact numeric floating-point results always have the same sign as the unrounded result.

Both conversions are defined to be computational operations in clause 5, so again they should be performed according to the prevailing rounding mode.

I would argue that Snow Leopard has the correct behavior here (assuming that it is correctly rounding the results according to the prevailing rounding mode). If you want to force the old behavior, you can always wrap your printf calls in code that changes the rounding mode, I suppose, though that's clearly not ideal.

Alternatively, you could use the %a format specifier (hexadecimal floating point) on C99 compliant platforms. Since the result of this conversion is always exact, it will never be effected by the prevailing rounding mode. I don't think that the Windows C library supports %a, but you could probably port the BSD or glibc implementation easily enough if you need it.

Stephen Canon
Thanks for the exhaustive answer. For the integer -> double conversion, I am going to try to replace `f=b;` by `f=(double)(b`, which should give the same behavior everywhere—each conversion being exact, the rounding decision is made by the double + and should always be according to prevailing rounding mode.
Pascal Cuoq
@Pascal: Yes, that should work (and is actually how int64 -> double conversion is implemented on some platforms). It also occurs to me, however, that Intel has *two* sets of rounding control bits (for x87 and for SSE). I know that `fesetround` on Snow Leopard sets both of them; is it possible that it fails to set both control words on the other platforms, and that some of the conversions are being done using the unit for which the control word isn't set? It may be worth looking into that possibility.
Stephen Canon