0

I am tryling to learn C and now i am a floats. I know all values have different sizes on different machines. But i am trying to learn the concept.

Lets say a float is saved in 4 bytes. I read that the first bit is the sign bit, which decide if the number is positve or negative. Then the next 7 bits is the exponent and the rest (23 bits) is the mantissa. Which could be this: 1 010 1001 0000 0000 1011 1001 0000 111

But how does this translate to a float? I can read binary but i dont see the formula behind it :). Could some one explain this one to me, or how it works?

2

2 Answers 2

2

I wrote this a few years ago.
I think it should help you understand how floating point numbers are represented:

http://www.eosgarden.com/en/articles/float/

Basically, the formula for normalized numbers is: -1^S * 1.M * 2^( E - 127 ) where S is the sign, M the mantissa, and E the exponent. But everything is explained in details in the previous link.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! The only part where i am a little bit stuck is this: quote from your article you go from this -10 · 1.0111 · 22 to this Mathematically, this means: 1 · ( 1 · 20 + 0 · 2-1 + 1 · 2-2 + 1 · 2-3 + 1 · 2-4 ) · 22 ( 20 + 2-2 + 2-3 + 2-4 ) · 22 22 + 20 + 2-1 + 2-2 4 + 1 + 0.5 + 0.25
Guess my math isn't that good to make that jump without some hints
The mantissa is expressed as binary scientific notation, so this is the computation of the real value. As for a normal binary number, the formula is: ( bit_1_value * 2 ^ bit_1_position ) + ( bit_2_value * 2 ^ bit_2_position ) + .... But as this is scientific notation, and as we have a comma, the bit position value is decremented, hence the -1, -2, etc.
Please excuse my lack of reading. Your article said it all i just read it like a rtard. =) Thank you again
No worries : ) Nice to know the article was helpful.
|
1

There are 8 bits in the exponent: seee eeee emmm mmmm mmmm mmmm mmmm mmmm

The formula is: (-1)^s * 1.M * 2^(E - 127)

I used big M and E to represent all the m and e bits, respectively. The M part is binary, the E part is decimal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.