I have a unsigned long integer value which represents a float using IEEE-754 format. What is the quickest way of printing it out as a float in C++?
I know one way, but am wondering if there is a convenient utility in C++ that would be better.
Example of the way that I know is:
union
{
unsigned long ul;
float f;
} u;
u.ul = 1084227584; // in HEX, this is 0x40A00000
cout << "float value is: " << u.f << endl;
(This prints out "float value is: 5" )
swegi had the right direction, but missed one character. The correct way is
The union method you suggested is the usual route that most people would take. However, it's technically undefined behavior in C/C++ to read a different member from a union than the one that was most recently written. Despite this, though, it's well-supported among pretty much all compilers.
Casting pointers, as Jon Skeet suggested, is a bad idea -- that violates the strict aliasing rules of C. An aggressive optimizing compiler will produce incorrect code for this, since it assumes that pointers of types
unsigned long *
andfloat *
will never alias each other.The most correct way, in terms of standards compliance (that is, not invoking undefined behavior), is to cast through a
char*
, since the strict aliasing rules permit achar*
pointer to alias a pointer of any other type:Though honestly, I would just go with the union method. From the cellperformance.com link above:
It will only work on 32-bit machines or machines where sizeof(long) == sizeof(float). On modern machines you might want to use int instead ... and you really have to take care if you're performing this trick.
I was thinking the same thing as Jon Skeet, but he beat me to it. However, I would do it a little more concisely than he did.
You get a pointer to your unsigned long, then reinterpret that as a pointer to float, then dereferencing your pointer to float produces the float value.
Since you are doing this in C++, it is clearer to use reinterpret_cast instead of the old C-style cast.
EDIT: I previously thought this was "just nasty", but it turns out to be worse than I thought - see the comments for details. I wouldn't recommend this approach, but I'm leaving it here to document the possibility (and the caveat!)
You can use pointers:
(This can be condensed into a single line, but I think it's clearer not to.)
Basically the same thing as your union... and equally dodgy unless you're sure of the sizes of the types involved.
If your platform supports them, you should probably use the size-specific type names for both the integer and floating point types.