C++ 中的双精度,308 位还是 15 位?
如果 double 可以表示最大 3.4E308(308 个零)的值,那么为什么我们说 double 只存储 15 位数字呢?说“308的十次方”有什么意义?
If double can represent value up to 3.4E308 (308 zeros), then why do we say that double stores only 15 digits? What is point of saying "ten power of 308" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们不会说“
double
仅存储 15 位数字”。我们说“double
有 15 位精度”。这意味着double
的计算值在打印为以 10 为基数的数字序列时,最多只能精确到这 15 位数字。double
可以代表3.4E308
。是的,要打印它,您需要超过 15 位的精度。由于浮点实现,某些特定值具有这些保证。 但是,例如,一个数字3.4E308 - 1
,它在double
的范围内,< em>不能用double
准确表示。如果您想确定,只需取
double
的前 15 位数字即可。有些值可以用超过 15 位数字正确表示,但有些则不能。double
范围内的每个值将正确表示到其十进制表示形式的第 15 位数字。We don't say that "
double
stores only 15 digits". We say that "double
has 15 digits of precision". It means that the computed value ofdouble
, when printed as a base-10 sequence of digits, is accurate only up to those 15 digits.double
can represent3.4E308
. Yes, to print it you need more than 15 digits of precision. Some particular values have those guarantees thanks to floating-point implementation. But, for example, a number3.4E308 - 1
, which is inside ofdouble
's range, cannot be represented accurately by adouble
.If you want to be sure, just take the first 15 digits of
double
. Some values can be correctly represented with more than 15 digits, but some cannot. Every value indouble
's range will be correctly represented up to the 15th digit of its decimal representation.为了帮助您以简单的方式理解这一点,请考虑一个可以表示以下数字的数字类型:
该类型可以表示最多 100000 的数字。该类型的精度是 6 位还是 1?
To help perceive this in simple terms, consider a number type that can represent following numbers:
The type can represent numbers up to 100000. Does this type have 6 digits of precision, or 1?