gpt4 book ai didi

java - 乘以 0.0000001 与除以 10000000 相同吗?

转载 作者:搜寻专家 更新时间:2023-10-30 19:42:30 26 4
gpt4 key购买 nike

在 Java 中, double 乘以 0.0000001 与除以 10000000 相同吗?我的直觉是,可能存在差异,因为 0.0000001 不能准确地用 double 表示。

最佳答案

不,由于您提到的原因,这不一样。这是一个例子:

double x = 894913.3;
System.out.println(x * 0.0000001); // prints 0.08949133
System.out.println(x / 10000000); // prints 0.08949133000000001

使用 BigDecimal,我们可以看到两个值之间的差异:

System.out.println(new BigDecimal(0.0000001));
System.out.println(new BigDecimal((double)10000000));

输出:

9.99999999999999954748111825886258685613938723690807819366455078125E-8
10000000

关于java - 乘以 0.0000001 与除以 10000000 相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487320/

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