Why this assertion fails in Java:
double eps = 0.00000000000001;
double ten = 10.0;
double result = (ten - (ten - eps));
Assert.assertTrue(result <= eps);
If I remove one zero before digit 1 in eps
, the assertion passes. I assume that this is related to the floating point implementation, but I'm not sure exactly how.
Also, if I replace digit 1 with 2 (like 0.00000000000002) the assertion passes as well. In that case, I can even add more zeros before the digit 2, the test will still pass. I tried with Double.MIN_VALUE
(4.9E-324) and the assertion also passed.
Can someone, please, explain in more details:
- Why the assertion passes with eps = 1.0E-13 but not with eps = 1.0E-14
- Why the assertion passes with eps =
Double.MIN_VALUE
(4.9E-324) and not with eps = 1.0E-14
EDIT: The assertion also fails when I increase the eps
to 1.0E-8: double eps = 0.00000001;