help-gplusplus
[Top][All Lists]
Advanced

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

Re: int/float binary conversion


From: float_dublin
Subject: Re: int/float binary conversion
Date: Thu, 15 Sep 2005 13:14:59 +0400
User-agent: Mozilla Thunderbird 1.0 (Windows/20041206)

Lars Christian Jensen wrote:

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

I gues the only way to find out is to look asm.


reply via email to

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