This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: float to int conversion
- From: "Warlich, Christof" <christof dot warlich at siemens dot com>
- To: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Mon, 22 Apr 2013 08:48:55 +0000
- Subject: RE: float to int conversion
- References: <6D83E89737156549AEA25EF9ED712C5DD11B at DEFTHW99EK1MSX dot ww902 dot siemens dot net> <5174F57B dot 4080701 at redhat dot com>
Andrew Haley [aph@redhat.com] 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.