Converting between IEEE 754 and Float (Format related

时间:2023-12-27 22:26:43

The float can be converted to well known single-precision IEEE 754 number, why 754? It's the standard representation of single-precision numbers, used in net transmission.

Compares to send the float as text or send/receive without re-formatting/un-formatting, it's much faster & safer. 

What confuses me is that nowadays floats are stored just what IEEE 754 says to be. Or maybe windows only?

But the most important thing is, when sending/receiving data/numbers, use standards.

See this page for more info.

I'll explain this format a little. It uses the first bit as a sign representation(1 if negative), the following 8bit as its exponential part, the last 23bit as its fraction part.

See this example:

1 10000001 10110011001100110011010

sign   expo       mantissa

It is divided as stated above.

The Microsoft's floats is following this standard. See the following definition of float in Windows.

Where float uses 8 bits of exponent and 23 bits of mantissa, compares to double's 11 bits of exponent and 52 bits of mantissa.

About the bias refered to in the page above:

QUOTE

Because exponents are stored in an unsigned form, the exponent is biased by half its possible value.For type float, the bias is 127; for type double, it is 1023.You can compute the actual exponent value by subtracting the bias value from the exponent value.

/QUOTE

Note that this exponent's base is not 10, of course it's 2! So 2E127 is approximately 10E38, that's why float's range is about 1E-38~3E38!