问题描述
可以存储在 IEEE 754 double 类型中且不丢失精度的最大无浮点"整数是多少?
What is the biggest "no-floating" integer that can be stored in an IEEE 754 double type without losing precision ?
推荐答案
在不丢失精度的情况下可以存储在 double 中的最大/最大整数与 double 的最大可能值相同.也就是说,DBL_MAX
或大约 1.8 × 10(如果您的双精度数是 IEEE 754 64 位双精度数).它是一个整数.它被准确地表现出来.你还想要什么?
The biggest/largest integer that can be stored in a double without losing precision is the same as the largest possible value of a double. That is, DBL_MAX
or approximately 1.8 × 10 (if your double is an IEEE 754 64-bit double). It's an integer. It's represented exactly. What more do you want?
继续,问我最大的整数是多少,这样它和所有较小的整数都可以存储在 IEEE 64 位双精度中而不会丢失精度.IEEE 64 位双精度数有 52 位尾数,所以我认为它是 2:
Go on, ask me what the largest integer is, such that it and all smaller integers can be stored in IEEE 64-bit doubles without losing precision. An IEEE 64-bit double has 52 bits of mantissa, so I think it's 2:
- 2 + 1 无法存储,因为开头的 1 和结尾的 1 中间有太多的零.
- 可以存储小于 2 的任何值,其中 52 位明确存储在尾数中,然后指数实际上为您提供另一个.
- 2 显然可以存储,因为它是 2 的小幂.
- 2 + 1 cannot be stored, because the 1 at the start and the 1 at the end have too many zeros in between.
- Anything less than 2 can be stored, with 52 bits explicitly stored in the mantissa, and then the exponent in effect giving you another one.
- 2 obviously can be stored, since it's a small power of 2.
或者另一种看待它的方式:一旦去除了指数的偏差,并忽略与问题无关的符号位,double 存储的值是 2 的幂,加上一个 52 位整数乘以 2.因此,使用指数 52,您可以存储从 2 到 2 − 1 的所有值.然后使用指数 53,您可以在 2 之后存储的下一个数字是 2 + 1 × 2.所以精度损失首先发生在 2 + 1.
Or another way of looking at it: once the bias has been taken off the exponent, and ignoring the sign bit as irrelevant to the question, the value stored by a double is a power of 2, plus a 52-bit integer multiplied by 2. So with exponent 52 you can store all values from 2 through to 2 − 1. Then with exponent 53, the next number you can store after 2 is 2 + 1 × 2. So loss of precision first occurs with 2 + 1.
这篇关于可以存储在 double 中的最大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!