微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 当l(long) – = f(float)时发生了什么?

参见英文答案 > Floating point arithmetic not producing exact results 7个
> Why does Java implicitly (without cast) convert a `long` to a `float`?4个
public class SimplePrint {

public static void main(String[] args) {
    long i = System.currentTimeMillis();
    System.out.println(i);
    float h = 0.0f;
    i -= h;
    System.out.println(i);
  }
}

输出是:

1477904636902

1477904695296

但是当我改变h变量的数据类型时

public class SimplePrint {

public static void main(String[] args) {
    long i = System.currentTimeMillis();
    System.out.println(i);
    double h = 0.0f;
    i -= h;
    System.out.println(i);
  }
}

输出改变了:

1477904677513

1477904677513

为什么是这样 ???

解决方法

JLS Sec 15.26.2中所述,复合赋值运算符E1 op = E2相当于
E1 = (T) ((E1) op (E2))

其中T是E1的类型.

所以,你在第一种情况下所做的是:

i = (long) (i - 0.0f)

为了评估 –,我必须被转换成浮点数,如JLS Sec 15.18.2所述:

Binary numeric promotion is performed on the operands (§5.6.2).

5.6.2

Otherwise,if either operand is of type float,the other is converted to float.

问题是i的值不能精确地表示为float:因为float只有24位有效数字(see here),所以只能精确表示大约2 ^ 24(= 16777216)的值;但目前的毫秒时间(至少在我的机器上)大约是1477905410000,这是更大的.

因此,在转换为float时会失去精度,并且在转换为long时无法恢复精度.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐