Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 147
Here are some examples of the IEEE 754 32-bit formats shown in Table 5-5. Note that 
the Most Significant Bit (MSb) of the mantissa column (i.e., the bit to the left of the radix 
point) is the implied bit, which is assumed to be 1 unless the exponent is zero (in which 
case the float is zero).
Use the following process to manually calculate the 32-bit example in Table 5-5.
The sign bit is zero; the biased exponent is 251, so the exponent is 251-127=124. Take 
the binary number to the right of the decimal point in the mantissa. Convert this to dec-
imal and divide it by 2
23
 where 23 is the number of bits taken up by the mantissa, to 
give 0.302447676659. Add 1 to this fraction. The floating-point number is then given 
by:
-1
0
2
124
1.302447676659
which becomes:
1
2.126764793256e+371.302447676659
which is approximately equal to:
2.77000e+37
Binary floating-point values are sometimes misunderstood. It is important to remember 
that not every floating-point value can be represented by a finite sized floating-point 
number. The size of the exponent in the number dictates the range of values that the 
number can hold, and the size of the mantissa relates to the spacing of each value that 
can be represented exactly. Thus the 24-bit format allows for values with approximately 
the same range of values representable by the 32-bit format, but the values that can be 
exactly represented by this format are more widely spaced.
So, for example, if you are using a 24-bit wide floating-point type, it can exactly store 
the value 95000.0. However, the next highest number it can represent is 95002.0 and 
it is impossible to represent any value in between these two in such a type as it will be 
rounded. This implies that C code which compares floating-point type may not behave 
as expected. For example:
volatile float myFloat;
myFloat = 95002.0;
if(myFloat == 95001.0)     // value will be rounded
PORTA++;                // this line will be executed!
in which the result of the if() expression will be true, even though it appears the two 
values being compared are different.
Compare this to a 32-bit floating-point type, which has a higher precision. It also can 
exactly store 95000.0 as a value. The next highest value which can be represented is 
(approximately) 95000.00781.
The characteristics of the floating-point formats are summarized in Table 5-6. The sym-
bols in this table are preprocessor macros which are available after including 
<float.h>
 in your source code. 
TABLE 5-5:
FLOATING-POINT FORMAT EXAMPLE IEEE 754
Format
 Number
 Biased exponent
 1.mantissa
 Decimal
32-bit
7DA6B69Bh
 
11111011b
1.0100110101101101
0011011b
2.77000e+37
(251)
(1.302447676659)
24-bit
42123Ah
 
10000100b
1.001001000111010b
36.557
(132)
(1.142395019531)