Reputation: 2265
I'm doing unit testing and I've got this line:
assertEquals(1.1886027926838422606868849265505866347, 1.18860279268384230000000000000000000000,0);
With a delta of 0 they should have to be exactly the same in order to pass and they are clearly not, however this test passes, try it yourself.
Changing the delta to 1E-50 still passes.
Why is it passing when they are two very different numbers?
Upvotes: 3
Views: 66
Reputation: 726569
This is because Java compiler rounds these two numeric literals to the same number.
Run this experiment:
System.out.println(1.1886027926838422606868849265505866347);
System.out.println(1.18860279268384230000000000000000000000);
This prints the same number (demo):
1.1886027926838423
1.1886027926838423
The double
primitive type can only handle up to 16 decimal places, so it cannot represent these numbers all the way to the last digit.
If you want full precision, use BigDecimal
instead.
Upvotes: 10
Reputation: 182761
The difference between the two numbers is too small to represent, so they compare as equal. You get roughly 16 decimal digits of precision.
Upvotes: 0