3.3.4.1 Why Are My Floating-Point Results Not Quite What I Am Expecting?
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 a
32-bit floating-point type, while double
and long
double
types are a 64-bit floating-point type.
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.