java - Why are these different numbers the same? -


i'm doing unit testing , i've got line:

assertequals(1.1886027926838422606868849265505866347, 1.18860279268384230000000000000000000000,0); 

with delta of 0 should have same in order pass , not, test passes, try yourself.

changing delta 1e-50 still passes.

why passing when 2 different numbers?

this because java compiler rounds these 2 numeric literals same number.

run experiment:

system.out.println(1.1886027926838422606868849265505866347); system.out.println(1.18860279268384230000000000000000000000); 

this prints same number (demo):

1.1886027926838423 1.1886027926838423 

the double primitive type can handle 16 decimal places, cannot represent these numbers way last digit.

if want full precision, use bigdecimal instead.


Comments

Popular posts from this blog

json - Zend error Connection -

javascript - Trigger mouseenter when an animated element touches mouse -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -