【发布时间】:2026-01-31 21:05:02
【问题描述】:
public class Exponents {
public static void main(String[] args) {
float expFloat = 1.38e-43f; // 1.38 here
System.out.println(expFloat);
}
}
输出:
1.37E-43
这太疯狂了。输出应该是 1.38 ,其他输出工作正常。 1.39 显示 1.39,1.37 显示 1.37,但 1.38 显示 1.37 --- 我的意思是这怎么可能?
更多的实验显示--- 1.47 显示 1.47,1.49 显示 1.49 但 1.48 显示 1.49 。 此外, 1.58 显示 1.58 ,但 1.59 显示 1.58 。
【问题讨论】:
-
每个人都知道浮点很疯狂,直到人们不再认为它很疯狂。
-
加倍,浮点数不适合
-
感谢@DnR 格式化问题。但是你能解释一下为什么会这样吗?
-
你为什么使用浮点数而不是双精度数?除非您有充分的理由使用 float,否则您应该始终使用 double。您正在使用非规范化数字,其中 float 会失去精度。
-
你试过
1.38e-44f吗?
标签: java floating-point floating-accuracy floating-point-precision