c++ - How to output IEEE-754 format integer as a float -
I have an unsigned long integer value that represents a float using the IEEE-754 format. What is the fast way to print it as a float in C ++?
I know one side, but I'm surprised that there is a convenient utility in C ++ that would be better.
The example I know is the example:
union (unsigned long ul; float f;) u; u.ul = 1084227584; // in hex, This 0x40A00000 cot is <"float value:" & lt; Uf & lt;
(This is the "float value: 5"
The union method you suggested was the normal route, which most people take However, it is technically a different member than undefined behavior C / C ++ However, it has been written recently. In spite of this, though, it is well supported in many compilers.
Casting Points, like, is a bad idea - which is the rule of C An aggressive optimizing compiler will produce the wrong code for this because it assumes that the type indicator unsigned long *
and float *
Will not nominate.
In terms of standards, the most correct method is to follow compliance (which is not implementing undefined behavior), through a four *
, because strict aliasing rule is a < Code> four * indicator one of the other types indicator:
unsigned long ul = 0x40A00000; Float f; Four * moment = (four *) & amp; ul; // OK, four * four types of any type * four * PF = (four *) & amp; F; // OK, four * can any type memcpy surname (pf, pul, sizeof (float));
Although honestly, I just go with the union method from the top of the cellarformence dot link:
This is a very common idiom and all major compilers Practically supported, reading and writing to any member in any order is acceptable practice.
Comments
Post a Comment