gpt4 book ai didi

java - 改变行为可能导致精度损失

转载 作者:IT老高 更新时间:2023-10-28 21:06:27 35 4
gpt4 key购买 nike

在 Java 中,当你这样做时

int b = 0;
b = b + 1.0;

您可能会丢失精度错误。但是如果你这样做了,为什么会这样

int b = 0;
b += 1.0;

没有错误吗?

最佳答案

这是因为 b += 1.0; 等价于 b = (int) ((b) + (1.0));narrowing primitive conversion (JLS 5.1.3)隐藏在复合赋值操作中。

JLS 15.26.2 Compound Assignment Operators (JLS 第三版):

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

For example, the following code is correct:

short x = 3;
x += 4.6;

and results in x having the value 7 because it is equivalent to:

short x = 3;
x = (short)(x + 4.6);

这也解释了为什么下面的代码可以编译:

byte b = 1;
int x = 5;
b += x; // compiles fine!

但这不是:

byte b = 1;
int x = 5;
b = b + x; // DOESN'T COMPILE!

在这种情况下您需要显式转换:

byte b = 1;
int x = 5;
b = (byte) (b + x); // now it compiles fine!

值得注意的是,复合赋值中的隐式转换是谜题 9:Tweedledum 的主题,来自精彩的书籍 Java Puzzlers .以下是本书的部分摘录(为简洁起见略有编辑):

Many programmers think that x += i; is simply a shorthand for x = x + i;. This isn't quite true: if the type of the result is wider than that of the variable, the compound assignment operator performs a silent narrowing primitive conversion.

To avoid unpleasant surprises, do not use compound assignment operators on variables of type byte, short, or char. When using compound assignment operators on variables of type int, ensure that the expression on the right-hand side is not of type long, float, or double. When using compound assignment operators on variables of type float, ensure that the expression on the right-hand side is not of type double. These rules are sufficient to prevent the compiler from generating dangerous narrowing casts.

For language designers, it is probably a mistake for compound assignment operators to generate invisible casts; compound assignments where the variable has a narrower type than the result of the computation should probably be illegal.

最后一段值得注意:C# 在这方面要严格得多(参见 C# Language Specification 7.13.2 Compound assignment)。

关于java - 改变行为可能导致精度损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2696812/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com