Java 故障?减去数字?

发布于 2024-12-15 08:01:03 字数 98 浏览 5 评论 0原文

这是Java中的一个错误吗?

我去解这个表达式:3.1 - 7.1

我得到答案:-3.9999999999999996

这是怎么回事?

Is this a glitch in Java?

I go to solve this expression: 3.1 - 7.1

I get the answer: -3.9999999999999996

What is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

虫児飞 2024-12-22 08:01:03

可以在这里找到很好的解释。 http://www.ibm.com/developerworks/java/library/j -jtp0114/

浮点运算很少是精确的。虽然有些数字,例如
作为 0.5,可以精确地表示为二进制(以 2 为基数)的十进制(因为
0.5 等于 2-1),其他数字,例如 0.1,则不能。因此,浮点运算可能会导致舍入误差,从而产生
结果接近(但不等于)您可能得到的结果
预计。例如,下面的简单计算结果为
2.600000000000001,而不是 2.6:

双 s=0;

for (int i=0; i<26; i++)
    s+=0.1;
System.out.println(s); 

同样,乘以 .1*26 会得到与
将 .1 加到自身 26 次。舍入误差变得更加严重
当从浮点转换为整数时,因为转换为
整数类型会丢弃非整数部分,即使对于计算也是如此
“看起来”他们应该具有完整的价值观。例如,
以下声明:

<预置><代码> 双 d = 29.0 * 0.01;
System.out.println(d);
System.out.println((int) (d * 100));

将产生输出:

<前><代码> 0.29
28

这可能不是您一开始所期望的。

请参阅提供的参考以获取更多信息。

A great explanation can be found here. http://www.ibm.com/developerworks/java/library/j-jtp0114/

Floating point arithmetic is rarely exact. While some numbers, such
as 0.5, can be exactly represented as a binary (base 2) decimal (since
0.5 equals 2-1), other numbers, such as 0.1, cannot be. As a result, floating point operations may result in rounding errors, yielding a
result that is close to -- but not equal to -- the result you might
expect. For example, the simple calculation below results in
2.600000000000001, rather than 2.6:

double s=0;

for (int i=0; i<26; i++)
    s += 0.1;
System.out.println(s); 

Similarly, multiplying .1*26 yields a result different from that of
adding .1 to itself 26 times. Rounding errors become even more serious
when casting from floating point to integer, because casting to an
integral type discards the non-integral portion, even for calculations
that "look like" they should have integral values. For example, the
following statements:

  double d = 29.0 * 0.01;
  System.out.println(d);
  System.out.println((int) (d * 100));

will produce as output:

 0.29
  28  

which is probably not what you might expect at first.

See the provided reference for more information.

天涯离梦残月幽梦 2024-12-22 08:01:03

正如其他几个人所提到的,如果您想获得精确的十进制值(例如在实现货币应用程序时),则不能指望 double。相反,您应该做的是仔细查看 BigDecimal

BigDecimal a = new BigDecimal("3.1");
BigDecimal b = new BigDecimal("7.1");
BigDecimal result = a.subtract(b);
System.out.println(result);      // Prints -4.0

As mentioned by several others you cannot count on double if you would like to get an exact decimal value, e.g. when implementing monetary applications. What you should do instead is to take a closer look at BigDecimal:

BigDecimal a = new BigDecimal("3.1");
BigDecimal b = new BigDecimal("7.1");
BigDecimal result = a.subtract(b);
System.out.println(result);      // Prints -4.0
送君千里 2024-12-22 08:01:03

计算机是 100% 的,所以在数学世界中这是正确的,但对普通人来说则不然。 Java 不能在特定数字上出现错误,因为它只是以相同方式运行但具有不同输入的代码!

PS Google 如何对数字进行四舍五入

Computers are 100% so in the math world that is correct, to the average person it is not. Java cant have a error on a specific number as it is just code that runs the same way but has a different input!

P.S. Google how to round a number

逆光飞翔i 2024-12-22 08:01:03

浮点数的舍入误差

3 * 0.1 != 0.3 相同(至少当编译器没有折叠时)

rounding errors in floating points

same way that 3 * 0.1 != 0.3 (when it's not folded by the compiler at least)

溺ぐ爱和你が 2024-12-22 08:01:03

自动类型升级正在发生,这就是结果。

这里有一些学习资源。

http://docs.oracle.com/javase/specs /jls/se5.0/html/conversions.html

下一步是学习使用格式化程序将其格式化为给定的精度/要求。

Automatic type promotion is happening and that is the result.

Here is some resource to learn.

http://docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html

The next step would be is to learn to use formatters to format it to the given precision / requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文