Re: float to int conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/22/2013 09:48 AM, Warlich, Christof wrote:
> Andrew Haley [aph@xxxxxxxxxx] wrote:
>>    "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). If the value of the integral part
>>    cannot be represented by the integer type, the behavior is
>>    undefined."
> 
> Ok, fair enough. But then, what about my second example?:
> 
>>> $ cat test2.c
>>> #include <stdio.h>
>>> int main() {
>>>     float c = 0x7fffffbf;
>>>    float d = 0x7fffffc0;
>>>    printf("%d, %d\n", (int) c, (int) d);   
>>>    return 0;
>>> }
>>>
>>> $ gcc test2.c
>>> $ ./a.out
>>> 2147483520, -2147483648
> 
> There, the float _does_ fit into an integer by definition, but it still
> yields the wrong result. 

No, it isn't the wrong result.  Try this:

#include <stdio.h>
int main() {
    float c = 0x7fffffbf;
    float d = 0x7fffffc0;
    printf("%11.11g, %11.11g\n", c, d);
    printf("%d, %d\n", (int) c, (int) d);
    return 0;
}

Andrew.





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux