5

GCC 4.8.0 compiling in/for 32-bit.

I find the behavior of cases 2 and 6 to be confusing:

int16_t s16 = 0;
double dbl = 0.0;

s16 = (int16_t)((double)32767.0); // 1: s16 = 32767
s16 = (int16_t)((double)32768.0); // 2: s16 = 32767
s16 = (int16_t)((double)-32768.0); // 3: s16 = -32768
s16 = (int16_t)((double)-32769.0); // 4: s16 = -32768

dbl = 32767.0;
s16 = (int16_t)dbl; // 5: s16 = 32767
dbl = 32768.0;
s16 = (int16_t)dbl; // 6: s16 = -32768  
dbl = -32768.0;
s16 = (int16_t)dbl; // 7: s16 = -32768
dbl = -32769.0;
s16 = (int16_t)dbl; // 8: s16 = -32768

I realize it's implementation defined, but consistency would still be nice. Can anyone explain what's going on?

2
  • 2
    No need to cast 32767.0, 32768.0, -32768.0 to double, they are already of type double.
    – ouah
    Apr 16, 2013 at 15:03
  • @ouah Yes, I know. I did so to emphasize the behavior.
    – Ioan
    Apr 16, 2013 at 15:10

2 Answers 2

3

The behaviour is not implementation-defined, it's undefined, per 6.3.1.4 (1):

If the value of the integral part cannot be represented by the integer type, the behavior is undefined.61)

61) The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

The paragraph was identical in C99, just the footnote had a different number (50).

For undefined behaviour, it is not uncommon that the behaviour for expressions evaluated at compile time is different than runtime-evalued, for example

1 << width_of_type

is often evaluated to 0 if the shift distance is given as a constant expression, and to 1 if it is a runtime value.

The reasoning that leads to the different behaviours for the same code is, as far as I gathered, that since undefined behaviour is a licence for the compiler to produce anything, it may as well do the simplest and/or fastest thing, and the simplest/fastest thing during compilation can be different from the simplest/fastest thing during runtime.

5
  • Would it matter that I'm using the C99 standard? Other related questions have indicated the casting behavior (between integral types though) was implementation defined. Also, I'm using signed types.
    – Ioan
    Apr 16, 2013 at 14:57
  • No, C99 had the same formulation. It's only for conversions between integral types that the behaviour is implementation-defined. (The footnote emphasises that the behaviour is undefined also for unsigned target types, contrary to conversions between integral types, where the behaviour is completely defined when the target is unsigned.) Apr 16, 2013 at 15:01
  • I realize undefined means no answer, but I would hope the behavior could be consistent or perhaps hint at some reasoning...
    – Ioan
    Apr 16, 2013 at 15:13
  • 2
    Understandable, but it just isn't so. If I compile with optimisations, I get the same results for the last four as for the first four, so the same code behaves differently, depending on optimisation level. The reasoning, as far as I know, is "if the behaviour is undefined, just exploit it and do the fastest/simplest thing". Apr 16, 2013 at 15:51
  • 1
    @Ioan, the reasoning behind any undefined behavior is that the compiler is free to do what it thinks is easiest and/or best. The results can be surprising at times, but that comes with the territory. The inconsistency in this case is caused by compile-time constants vs. run-time variables. Apr 16, 2013 at 15:52
0

As answered already, "undefined" does not mean "implementation defined".

If there is a risk that your values will be out of the limits, so you should verify the range before the conversion. Or you can use some library, e.g. with boost math functions like itrunc(double) you should get boost::math::rounding_error when out of range (unfortunatelly I don't know whether you can make it work with your int16_t).

http://www.boost.org/doc/libs/1_53_0/libs/math/doc/sf_and_dist/html/math_toolkit/utils/rounding/trunc.html

For the reasoning why the behaviour may be different: Maybe the implementation copies the bits representing the value, it realizes that it is out of range and it quits without caring about the sign bit which stays set to a random value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.