3.3.4.1 Why Are My Floating-point Results Not Quite What I Am Expecting?

First, make sure that if you are watching floating-point variables in MPLAB IDE that the type and size of these match how they are defined. In MPLAB XC32, the float type is 32 bits wide, the double and long double types are 64-bits wide, as discussed in 9.4 Floating-Point Data Types.

Since floating-point variables only have a finite number of bits to represent the values they are assigned, they will hold an approximation of their assigned value. A floating-point variable can only hold one of a set of discrete real number values. If you attempt to assign a value that is not in this set, it is rounded to the nearest value. The more bits used by the mantissa in the floating-point variable, the more values can be exactly represented in the set and the average error due to the rounding is reduced.

Whenever floating-point arithmetic is performed, rounding also occurs. This can also lead to results that do not appear to be correct.