为什么Java中允许双重文字中的任意精度? [英] Why is arbitrary precision in double literals allowed in Java?

查看:121
本文介绍了为什么Java中允许双重文字中的任意精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚从 Peter Lawreys的帖子了解到这是有效的表达方式,并进行评估至 true

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.

推荐答案

问题是您输入的极少数小数可以完全表示为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.

这篇关于为什么Java中允许双重文字中的任意精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆