本文介绍了为什么双精度值的操作不会给出预期的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

    System.out.println(2.14656);

2.14656

    System.out.println(2.14656%2);

0.14656000000000002

0.14656000000000002

WTF?

推荐答案

做出预期的结果。您的期望不正确。

The do give the expected results. Your expectations are incorrect.

当您输入双精度字面值 2.14656 时,您实际获得的是最接近的双精度值,即:

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

2.14656000000000002359001882723532617092132568359375

println 在打印出来(到17位有效数字)时会发生这种情况,所以你看到你想要的。

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

在模数运算之后(这是准确的),值为:

After the modulus operation (which is exact), the value is:

0.14656000000000002359001882723532617092132568359375

再次,打印时这是四舍五入,但因为有一个较少的领先数字,圆点距离右边一位数,所以您看到尾随 2

Again, this is rounded when it gets printed, but because there is one less leading digit, the round point is one digit farther to the right, and so you see that trailing 2.

这篇关于为什么双精度值的操作不会给出预期的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:29