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 11:00:05 +0400
User-agent: Mozilla Thunderbird 1.0 (Windows/20041206)

Ulrich Eckhardt wrote:
Lars Christian Jensen wrote:

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

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


You are using stupid, brute force casts to convert between totally
unrelated objects and expect anything useful?

Sorry, but what exactly is your problem? If you wanted to demonstrate that
it is possible to write C++ programs that don't behave consistently (i.e.
show different implementations of 'undefined behaviour'), fine, but we all
knew that already.

Uli


what undefined behavor? if sizeof(int)==sizeof(float) f1==f2 must be bit copied.

to Lars Christian Jensen:
the only reason it could differ, I think is sizeof(int)!=sizeof(float),
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;
}



reply via email to

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