[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: int/float binary conversion
From: |
Lars Christian Jensen |
Subject: |
Re: int/float binary conversion |
Date: |
Thu, 15 Sep 2005 09:41:38 +0200 |
On Thu, 15 Sep 2005, float_dublin wrote:
the only reason it could differ, I think is sizeof(int)!=sizeof(float),
In which case 4!=4.
I have not been able reproducing the first bahavor (when f2 is 0). Can
you please try this (on your's (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5))
and post results:
#include <cstdio>
#include <cstdlib>
int main() {
float f1 = 4.5f;
char *i=(char*)malloc(4);
*((float*)i) = *((float*)&f1);
float f2 = *((float*)i);
printf("f1 = %f, f2 = %f\n", f1, f2);
free(i);
return 0;
}
$ c++ -O2 -march=i686 foo.cpp
$ ./a.out
f1 = 4.500000, f2 = 4.500000
But if I modify the program slightly:
*((float*)i) = *((float*)&f1);
to
*((int*)i) = *((int*)&f1);
$ c++ -O2 -march=i686 bar.cpp
$ ./a.out
f1 = 4.500000, f2 = 0.000000
--
Lars Christian Jensen