5.3.4 Floating-Point Data Types

The MPLAB XC8 compiler supports 32-bit floating-point types, with an IEEE 754 32-bit format. The table below shows the data types and their corresponding size and arithmetic type.

Table 5-2. Floating-point Data Types
Type (Standard)Size (bits)Arithmetic Type
float32Real
double32Real
long double32Real

Variables can be declared using the float and double keywords, respectively, to hold values of these types. Floating-point types are always signed and the unsigned keyword is illegal when specifying a floating-point type. Types declared as long double will use the same format as types declared as double. All floating-point values are represented in little-endian format with the LSB at the lower address.

The 32-bit floating-point type supports “relaxed” semantics when compared to the full IEEE implementation, which means the following rules are observed.

Tiny (subnormal) arguments to floating-point routines are interpreted as zeros. There are no representable floating-point values possible between -1.17549435E-38 and 1.17549435E-38, except for 0.0. This range is called the denormal range. Subnormal results of routines are flushed to zero. There are no negative 0 results produced.

Not-a-number (NaN) arguments to routines are interpreted as infinities. NaN results are never created in addition, subtraction, multiplication, or division routines where a NaN would be normally expected—an infinity of the proper sign is created instead. The square root of a negative number will return the “distinguished” NaN (default NaN used for error return).

Infinities are legal arguments for all operations and behave as the largest representable number with that sign. For example, +inf + -inf yields the value 0.

The format for the floating-point type is described in the following table, where:

  • sign is the sign bit, which indicates if the number is positive or negative
  • The biased exponent is 8 bits wide and is stored as excess 127 (i.e., an exponent of 0 is stored as 127).
  • mantissa is the mantissa, which is to the right of the radix point. There is an implied bit to the left of the radix point which is always 1 except for a zero value, where the implied bit is zero. A zero value is indicated by a zero exponent.

The value of this number is (-1)sign x 2(exponent-127) x 1.mantissa.

Table 5-3. Floating-point Formats
FormatSignBiased ExponentMantissa
IEEE 754 32-bitxxxxx xxxxxxx xxxx xxxx xxxx xxxx xxxx

Here are some examples of the IEEE 754 32-bit formats shown in the following table. 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.

Table 5-4. Floating-point Format Example IEEE 754
FormatValueBiased Exponent1.mantissaDecimal
32-bit7DA6B69Bh11111011b1.01001101011011010011011b2.77000e+37
(251)(1.302447676659)

Use the following process to manually calculate the 32-bit example in the above table.

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 decimal and divide it by 223 where 23 is the size of the mantissa, to give 0.302447676659. Add 1 to this fraction. The floating-point number is then given by:

-10 x 2124 x 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.

For example, if you are using a 32-bit floating-point type, it can exactly store the value 95000.0. However, the next highest number it can represent is (approximately) 95000.00781 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 values might not behave as expected.

For example, in the following code
volatile float myFloat;
myFloat = 95000.0;
if(myFloat == 95000.003)      // the literal value will be rounded to 95000.0
    PORTA++;                  // this line will be executed!
the result of the if controlling expression will be true, even though it appears the two values being compared are different.

The characteristics of the floating-point formats are summarized in the declarations for <float.h>, contained in the Microchip Unified Standard Library Reference Guide. The symbols presented there are preprocessor macros that are available after including <float.h> in your source code. As the size and format of floating-point data types are not fully specified by the C Standard, these macros allow for more portable code which can check the limits of the range of values held by the type on this implementation.