Reputation: 27
I am unable to understand float variable in c, so tried some code to and check how values are displayed in console,
I read many articles regarding but didn't satisfy me like this http://www.cquestions.com/2011/02/memory-representation-of-float-data.html says that exponent is 8 bit and mantissa is 24 bit, but if so then exponent part should vary for 0 to 256 or what i am confused. please help me in learning this
void main()
{
float f="different values below";
printf('float value is : %f',f);
}
tried values for f console output
257.123456 257.123444
256.123456 256.123444
128.123456 128.123459
100.123456 100.123459
10.123456 10.123456
Upvotes: 1
Views: 250
Reputation: 2306
Some key things to understand:
The 8 bits in the exponent are biased by 127. What does this mean? This means that if the value 127 appears in the exponent, it really means the exponent is logically zero. Similarly if 129 appears then the exponent is logically 2, and if 124 appears the exponent is logically -3. So the encoded, or logical, exponent can range from -127 to 128.
The mantissa is always assumed to be (and by definition) positive, and with an implicit 1 in front of it. So, if you see 10010... in the mantissa field, then the number being encoded there is 1.10010.
The high bit is the sign bit, if it's zero then the encoded value is positive and if it's one then the encoded value is negative.
There are a number of special values included in the format that don't fit this pattern. For instance the value zero is conveniently represented by all zero bits in every field. There are special values for NaNs (not a number) and infinity. There are also special encodings for denormals, these are values between zero and the lowest representable exponent of -127.
Example:
You see this value in memory:
00 00 B0 C0
It's a little endian system so it represents this hex value:
0xC0B00000
and this binary value:
1100 0000 1011 0000 0000 0000 0000 0000
Regrouping the bits by sign, exponent, mantissa fields the number looks like this:
1 10000001 01100000000000000000000
So the encoded number is:
-1.011 * 2^2
Since the exponent is positive 2, we move the radix point to the right by 2 places. Similarly negative exponents will move the radix point to the left by their magnitude. We rewrite the number again:
-101.1
Now lets convert it to decimal:
-(1*4 + 0*2 + 1*1 + 1*(1/2)) = -5.5
Upvotes: 0
Reputation: 26185
This is an analysis of one of your numbers, 10.123456. The binary representation, big-endian, is 0100_0001_0010_0001_1111_1001_1010_1101
The most significant bit is the sign bit. It is 0, indicating non-negative.
The next 8 bits, 10000010, decimal 130, are the exponent. It is stored in excess 127 form, so the effective exponent is 130-127 = 3.
The significand, for normal numbers, has the form 1.x where x is the last 23 bits. The 1 is not stored. Your significand is 1.010_0001_1111_1001_1010_1101.
Adjusting for a radix 2 exponent of 3, this becomes 1010.0001_1111_1001_1010_1101. The 1010 before the binary point corresponds to 10 before the decimal point. The fraction part is 129453/(2^20)=0.12345600128173828125. The exact value of your float, in decimal, is 10.12345600128173828125
Upvotes: 1
Reputation: 97948
You can write any float in scientific notation, say for 512.4, to get 0.5124 * 10^3. The exponent here is 3 and the mantissa is 5124. If you have 8 bits for the exponent then you can express n * (10^-128... 10^127). If n is 24 bits, then you roughly have -2^23 <= n <= 2^23
. In reality computers work with base 2 and not 10 but the idea is the same.
Also, since you are using base 2 and have limited number of bits, you can't express all possible float numbers in binary. This is why the input and output differs in some (most) of the cases.
Upvotes: 0