Why operations with double precision values ​​do not give the expected results?

    System.out.println(2.14656);

2.14656

    System.out.println(2.14656%2);

+0.14656000000000002

WTF?

+3
source share
3 answers

They give the expected results. Your expectations are wrong.

When you enter a double-precision literal 2.14656, what you actually get is the closest double-precision value, which:

2.14656000000000002359001882723532617092132568359375

println happens around this when it prints it (up to 17 significant digits), so you see the nice value that you expect.

After the operation of the module (which is accurate), the value:

0.14656000000000002359001882723532617092132568359375

, , , - , , , , 2.

+13

base-2 -10.

"" - 2, 4, 8, 16, 32, 64 .. , 10, 100, 1000, 10000 - "" -10.

. , 1.42E-13 2.11E47. "" -10. .

, . 100% , . , 100%. , int. , , , , .

, , , . , , 1/100, 1/32 1/20 .., . , , . -, , .

+3

Source: https://habr.com/ru/post/1768662/


All Articles