加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:56:29 所属栏目:Java 来源:网络整理
导读:参见英文答案 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.currentTime
参见英文答案 > 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时无法恢复精度.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读