I.e.:
|
0.2 = 0.001100110011001100110011... Because: 0.2 *2 ------- 0 0.4 *2 ------- 0 0.8 *2 ------- 1 1.6 --> 0.6 *2 ------- 1 1.2 --> 0.2 *2 ------- 0 0.4 *2 ------- 0 0.8 *2 ------- 1 1.6 --> 0.6 *2 ------- 1 1.2 --> 0.2 and so on (repeats !) |
In other words:
|
Because the IEEE 754 representation uses a finite number of bits, the representation is truncated which results in a round off error !!
To represent 1/3 in decimal, you will need an infinite number of (decimal) digits:
1/3 = 0.3333333333333333... |
When you truncate the representation to a finite number of digits, you will have a round off error also.
// /home/cs255001/demo/java/FloatError.java float f1, f2; f1 = 0.2f + 0.2f + 0.2f + 0.2f + 0.2f + 0.2f + 0.2f + 0.2f + 0.2f + 0.2f; f2 = 2.0f; if (f1 == f2) { System.out.println ("f1 == f2"); } else { System.out.println ("f1 != f2"); } |
Notice that:
f1 = 0.2 + 0.2 + .... + 0.2 (10 times) f2 = 2.0 They should be equal to each other !!! |
But due to round off errors, if you run this program, you will get:
f1 != f2 |
The reason is: when you print the values f1 and f2 out, you will get:
f1 = 2.000002 f2 = 2.0 |
|
How to run the program:
|