“可能的精度损失” Java 是疯了还是我错过了什么?

发布于 2024-08-31 20:59:54 字数 649 浏览 2 评论 0原文

我收到了“精度损失”错误,而实际上应该没有,AFAIK。

这是一个实例变量:

byte move=0;

这发生在此类的方法中:

this.move=(this.move<<4)|(byte)(Guy.moven.indexOf("left")&0xF);

移动是一个字节,移动仍然是一个字节,其余的被转换为一个字节。

我收到此错误:

[javac] /Users/looris/Sviluppo/dumdedum/client/src/net/looris/android/toutry/Guy.java:245: possible loss of precision
[javac] found   : int
[javac] required: byte
[javac]             this.move=(this.move<<4)|(byte)(Guy.moven.indexOf("left")&0xF);
[javac]                                         ^

我尝试了很多变体,但仍然收到相同的错误。

我现在已经毫无头绪了。

I'm getting a "loss of precision" error when there should be none, AFAIK.

this is an instance variable:

byte move=0;

this happens in a method of this class:

this.move=(this.move<<4)|(byte)(Guy.moven.indexOf("left")&0xF);

move is a byte, move is still a byte, and the rest is being cast to a byte.

I get this error:

[javac] /Users/looris/Sviluppo/dumdedum/client/src/net/looris/android/toutry/Guy.java:245: possible loss of precision
[javac] found   : int
[javac] required: byte
[javac]             this.move=(this.move<<4)|(byte)(Guy.moven.indexOf("left")&0xF);
[javac]                                         ^

I've tried many variations but I still get the same error.

I'm now clueless.

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

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

发布评论

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

评论(3

韵柒 2024-09-07 20:59:54

实际上,所有逻辑运算符 (& | ^) 都会返回 int,无论其操作数如何。您还必须转换 x|y 的最终结果。

Actually all logic operatos (& | ^) return an int, regardless of their operands. You have to cast the final result of x|y as well.

半世蒼涼 2024-09-07 20:59:54

这是因为 this.move<<4 返回一个 int。

当 Java 找到 移位运算符 它适用 一元促销 到每个操作数;在这种情况下,两个操作数都会提升为 int,结果也是如此。
其他 Java 运算符的行为类似;请参阅相关且具有指导性的讨论,“可能导致精度损失的不同行为”。

That's because this.move<<4 returns an int.

When Java finds a shift operator it applies unary promotion to each operand; in this case, both operands are promoted to int, and so is the result.
The behaviour is similar for other Java operators; see a related and instructive discussion, "Varying behavior for possible loss of precision".

呆头 2024-09-07 20:59:54

按位或操作数受二进制数字提升的影响。以下是其在 JLS 中的定义,

5.6.2 二进制数字提升

当运算符应用二进制时
数字提升为一对
操作数,每个操作数必须表示一个
数值类型的值,如下
使用扩展,按顺序应用规则
转换 (§5.1.2) 转换
必要的操作数:

  • 如果其中一个操作数为 double 类型,则另一个将转换为
    双倍。
  • 否则,如果其中一个操作数为 float 类型,则另一个将转换为
    漂浮。
  • 否则,如果其中一个操作数为 long 类型,则另一个为
    转换为长。
  • 否则,两个操作数都将转换为 int 类型。

如您所见,没有字节类型,因此默认情况下所有字节都提升为 int。你必须将其转换回字节才能消除警告,

this.move=(byte)((this.move<<4)|(Guy.moven.indexOf("left")&0xF));

The bitwise OR operands are subject to Binary Numeric Promotion. Here is how its' defined in JLS,

5.6.2 Binary Numeric Promotion

When an operator applies binary
numeric promotion to a pair of
operands, each of which must denote a
value of a numeric type, the following
rules apply, in order, using widening
conversion (§5.1.2) to convert
operands as necessary:

  • If either operand is of type double, the other is converted to
    double.
  • Otherwise, if either operand is of type float, the other is converted to
    float.
  • Otherwise, if either operand is of type long, the other is
    converted to long.
  • Otherwise, both operands are converted to type int.

As you can see, there is no byte type so all the bytes are promoted to int by default. You have to cast it back to byte to get rid of the warning,

this.move=(byte)((this.move<<4)|(Guy.moven.indexOf("left")&0xF));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文