Are there any whole numbers which the double cannot represent within the MIN/MAX range of a double?

你。 提交于 2019-12-03 10:36:11

Sure, there are whole numbers that are not representable as double-precision floating points.

All whole numbers not exceeding Pow(2, 53) or 9007199254740992, are representable. From Pow(2, 53) to Pow(2, 54) (that's 18014398509481984), only even numbers are representable. The odd numbers will be rounded.

Of course it continues like that. From Pow(2, 54) to Pow(2, 55) only the multiples of 4 (those whole numbers which 4 divides) are representable, from Pow(2, 55) to Pow(2, 56) only multiples of 8, and so on.

This is because the double-precision floating-point format has 53 bits (binary digits) for the mantissa (significand).

It is easy to verify my claims. For example, take the number 10000000000000001 as an integer64. Convert it to double and then back to integer64. You will see the precision loss.

When you take very large double-precision numbers, certainly a very little percentage of the whole numbers is representable. For example near 1E+300 (which is between Pow(2, 996) and Pow(2, 997)) we are talking multiples of Pow(2, 944) (1.4870169084777831E+284). This is consistent with the fact that a double is precise up to approximately 16 decimal figures. So a whole number with 300 figures will be "remembered" only by its first approx. 16 figures (actually 53 binary digits).


Addition: The first power of ten that is not exactly representable is 1E+23 (or 100 sextillions, short scale naming style). Near that number, only integral multiples of 16777216 (that is Pow(2, 24)) are representable, but ten to the 23rd power is clearly not a multiple of two to the 24th power. The prime factorization is 10**23 == 2**23 * 5**23, so we can divide evenly by two only 23 times, not 24 times as required.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!