为什么 Java 中允许双精度文字的任意精度?

发布于 2024-11-29 23:01:38 字数 755 浏览 2 评论 0原文

我刚刚从 Peter Lawreys 帖子 得知这是有效的表达式,并且计算结果为 真实。

333333333333333.33d == 333333333333333.3d

我的问题是,为什么允许使用不能用双精度表示的双精度文字,而不允许使用无法表示的整数文字。此决定的理由是什么?


附注,我实际上可以触发双精度数超出范围的编译错误:-)

99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999d

因此,只要我们在 (min, max) 范围内,文字就会得到近似值,但是当超出范围时其中,编译器似乎拒绝近似它。

I just learned from Peter Lawreys post that this is valid expression, and evaluates to true.

333333333333333.33d == 333333333333333.3d

My question is, why is it allowed to have double literals which can't be represented in a double, while integer literals that can't be represented are disallowed. What is the rationale for this decision.


A side note, I can actually trigger out of range compile error for doubles literals :-)

99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999d

So as long as we're in (min, max) range, the literal gets approximated, but when going outside of that, it seems the compiler refuses to approximate it.

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

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

发布评论

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

评论(2

疯狂的代价 2024-12-06 23:01:38

问题是您可能键入的小数很少可以完全表示为 IEEE 浮点数。因此,如果删除所有非精确常量,则使用双文字会变得非常笨拙。大多数时候,“假装我们可以代表它”的行为要有用得多。

The problem is that very few decimals that you might type can be represented exactly as an IEEE float. So if you removed all non-exact constants you would make using double literals very unwieldy. Most of the time the behaviour of "pretend we can represent it" is far more useful.

紙鸢 2024-12-06 23:01:38

主要原因可能是 Java 根本无法判断何时超出精度,因为没有相应的 CPU 操作代码。

为什么没有 CPU 标志或类似标志?因为数字的表示根本不允许这样做。例如,即使是像“0.1”这样的简单数字也没有明确的表示。 0.1 给你“00111111 10111001 10011001 10011001 10011001 10011001 10011001 10011010”(参见 http://www.binaryconvert.com/result_double.html?decimal=048046049)。

该值并不精确为 0.1,而是 1.00000000000000005551115123126E-1

因此,即使对于这些“简单”的情况,代码也必须抛出异常。

The main reason is probably that Java simply can't tell when you're running out of precision because there is no CPU op code for that.

Why is there no CPU flag or similar? Because the representation of the number simply doesn't allow it. For example even simple numbers like "0.1" have no definite representation. 0.1 gives you "00111111 10111001 10011001 10011001 10011001 10011001 10011001 10011010" (see http://www.binaryconvert.com/result_double.html?decimal=048046049).

That value isn't precisely 0.1 but 1.00000000000000005551115123126E-1.

So even for these "simple" cases, the code would have to throw an exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文