help-gplusplus
[Top][All Lists]
Advanced

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

int/float binary conversion


From: Lars Christian Jensen
Subject: int/float binary conversion
Date: Tue, 13 Sep 2005 10:39:37 +0200


The program below prints "f1 = 4.500000, i = 0x08048484, f2 = 0.000000", when compiled with c++ (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5). This don't looks like un-defined behaviour to me, but the same program outputs "f1 = 4.500000, i = 0x40900000, f2 = 4.500000", when compiled with other compilers.


#include <cstdio>

int main() {
   float f1 = 4.5;
   int i =    *((int*)&f1);
   float f2 = *((float*)&i);

   printf("f1 = %f, i = 0x%08x, f2 = %f\n", f1, i, f2);

   return 0;
}

--
Lars Christian Jensen


reply via email to

[Prev in Thread] Current Thread [Next in Thread]