java双精度似乎不一致

时间:2018-09-29 15:17:34

标签: java precision

我很感谢您的评论和参考,但我仍然不了解如何存储双精度型。我确实了解浮点数的工作原理,并认为double的效果相同,但是使用了另一个字节来提高精度。简而言之,这对我来说毫无意义:

 double a = 5.01 is stored as 5.01 how is it able to do that? 
 since .1 can't be represented in binary?

此问题不是所引用问题的重复。在第一种情况下,.01存储为.0099999999787,在第二种情况下,.01存储为.01000000000000

println有时会绕一圈的评论似乎是合理的,但计算值似乎确实是.0100000000000

我有一个我不理解的以下示例:

double a = 6;
double b = 5.99;
double c = .99;

System.out.println((a-b) + "  " + (a-c));

why is a - b = .0099999999999787  (which seems correct)
but    a - c = 5.01 (which seems incorrect)

shouldn't a - c = 5.0099999999787 ?

0 个答案:

没有答案