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?
32767.0
,32768.0
,-32768.0
todouble
, they are already of typedouble
.