# Thread: Floating-Point Numbers

1. ## Floating-Point Numbers

Greetings All-

I am currently reading about floating-point numbers. As per the C Primer Plus, a floating-point representation involves breaking up a number into a fractional part and an exponent part and storing the parts separately.

Example mentioned was how 7.0 is written as fractional part and exponent part:

7.0 as 0.7E1. 0.7 as the fractional part and 1 as the exponent part.

My question is:

Is it incorrect to break 7.0 as 7.0E0, 7.0 as the fractional part and 0 as the exponent part?

Your thoughts would be greatly appreciated.

Cheers
MindLess

2. Whether you display 7.0 or 7.0E0 is a presentation issue, not a storage issue.

Internally, floats always have a fractional part and an exponent part.

3. fractional part and an exponent part
isn't quite right. There's an exponent and a significand/mantissa.
Think of the mantissa as a value in the range [1.0, 2.0). Each bit represents a binary fraction.
The first bit is always a 1.
The next bit optionally adds half.
The next bit optionally adds a quarter.
The next bit optionally adds an eighth.
The next bit optionally adds a 1/16...
Then according to the exponent, all of those bits are doubled or halved some number of times.

4. Greetings All-

Thank you Salem and iMalc for your replies. I am struggling with the concept of floating-point data types.

Example:
Code:
float salary;
From what I read, float uses 32-bits and accurately represents the first 6 significant digits and has a range from 10^-37 to 10^+37. I used the sizeof() operator to see how many bytes float is represented with. The result was 4 bytes i.e. 32-bits.

And 8-bits for Exponent [ for sign/value ], and 24-bits for the Mantissa/Significand [ for sign/value ].

Here are my questions:

8-bits for Exponent: Shouldn't the valid range be from -128 to +127?
24-bits for Mantissa: Does it mean the valid values are from -8388608 to +8388607?

In my example code, say a user inputs 5000 for salary. Would it be correct to say that the Mantissa would be 5000 and the Exponent would be 0?

/Edit: I tried reading the information in the link
http://www.cprogramming.com/tutorial...ing_point.html I am not able to understand the floats. Not sure what to do with current situation.

Cheers
MindLess

5. > 8-bits for Exponent: Shouldn't the valid range be from -128 to +127?
pow(2,127) is about the same as pow(10,37)

> 24-bits for Mantissa: Does it mean the valid values are from -8388608 to +8388607?
23 bits, and it represents a fraction, not a whole number.

> In my example code, say a user inputs 5000 for salary.
> Would it be correct to say that the Mantissa would be 5000 and the Exponent would be 0?
The mantissa represents a normalised value between [1.0,2.0), and raised to some power of two (the exponent). You can see from the program below that powers of two have all the mantissa bits zero, and increasing by a power of two just increments the exponent. Note that the exponent is biased so that exponents don't need to store an explicit sign bit (exponents of 00 and FF are reserved for things like infinity)

Code:
#include<stdio.h>
int main ( ) {
float arr[] = { 0.125, 0.25, 0.5, 1, 2, 4, 44 };
union {
float           f;
unsigned long   d;
} a;
int i;
for ( i = 0 ; i < sizeof(arr)/sizeof(*arr) ; i++ ) {
unsigned long sign, exponent, mantissa;
a.f = arr[i];
sign     = ( a.d >> 31 ) & 0x01;
exponent = ( a.d >> 23 ) & 0xFF;
mantissa = a.d & 0x7FFFFF;
/* sign is only ever 0 or 1 */
/* exponent is 23 bits, but 24 are printed, so ignore the MSB */
printf("&#37;1lx:%02lx:%06lx is %f\n", sign, exponent, mantissa, a.f );
}

return 0;
}

0:7c:000000 is 0.125000
0:7d:000000 is 0.250000
0:7e:000000 is 0.500000
0:7f:000000 is 1.000000
0:80:000000 is 2.000000
0:81:000000 is 4.000000
0:84:300000 is 44.000000
42 being 32 + 8 + 4
Taking the first nibble of the exponent (0011), we ignore the first bit (see the code comment), then we're left with 011

As a fraction, this is no half, + 1 quarter + 1 eighth (=0.375). Since we don't store the most significant bit, the true value of the mantissa is 1.375.

To get back to our decimal value, we now multiply this by some power of two (in this case, 2^5, or 32).
1.375 * 32 is indeed 44

Popular pages Recent additions