Problem
I am using floating point numbers in my program, but the results of some floating point arithmetic is not what I would expect. For example, (45.2 - 45.1) does not yield the expected result of 0.1.
It would also seem that some floating numbers are not accurately represented. For example, when I use printf(S) to print out 45.2, it displays 45.2000000000000028000000000.
Solution
It is important to understand how floating point values are represented in order to appreciate the full consequences of this problem.
A float, for example, is a 32-bit value (with a mantissa and exponent) which has an intended range of about -3.4*(10^38) to +3.4*(10^38). However, the 4 bytes used for the float restrict the number of values that can be represented to approximately four thousand million. For this reason, there will be many floating point values that cannot be accurately represented using this type of encoding.
Therefore, in many cases, a floating point value is encoded as the _closest_ number that can be represented.
The following example program demonstrates the current representable floating point values for 45.1, as well as the closest encodings for the next and previous floating point values:
#include <math.h>main()
{
double x, y;x = 45.1;printf("curr fp value: %a, %.25f\n", x, x);
y=nextafter(x, x+1); printf("next fp value: %a, %.25f\n", y, y);
y=nextafter(x, x-1); printf("prev fp value: %a, %.25f\n", y, y); }
which produces the following output:curr fp value: 0x1.68ccccccccccdp+5, 45.1000000000000014000000000 next fp value: 0x1.68ccccccccccep+5, 45.1000000000000085000000000 prev fp value: 0x1.68cccccccccccp+5, 45.0999999999999943000000000
Thus, for the arithmetic expression (45.2 - 45.1), this would be encoded as:
(45.2000000000000028000000000 - 45.1000000000000014000000000)which yields the result of (0.1000000000000014000000000) and not simply 0.1 as might be logically expected.
Therefore, it cannot be considered valid to use a float in such a way that its actual value is assumed to be of a specific precision. It is recommended, instead, that a more correct way is to employ a range of precision for floats. For example:
#include <math.h>
#include <float.h>/* cc -o diff diff.c -lm */main()
{
double diff;diff = 45.2 - 45.1;diff -= 0.1;if ( diff == 0 ) printf( "45.2 - 45.1 == 0.1\n" ); else printf( "45.2 - 45.1 != 0.1\n" );
if ( fabs(diff) < FLT_EPSILON ) printf( "45.2 - 45.1 approx == 0.1\n" ); else printf( "45.2 - 45.1 approx != 0.1\n" ); }
which produces the following output:45.2 - 45.1 != 0.1 45.2 - 45.1 approx == 0.1
NOTE: The constant FLT_EPSILON appears in the above program.
This, and many other constants pertaining to floating
point arithmetic and representation, are defined in
/usr/include/float.h. FLT_EPSILON is the smallest floating point value greater than 0.0 that can be represented on the
Intel family of processors, such that the expression
(FLT_EPSION + 1.0) no longer equals 1.0. It is supplied in
/usr/include/float.h in accordance with the ANSI standard
for the C programming language, which requires that all
implementations of C include such limits.