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

前端 未结 1 417
滥情空心
滥情空心 2021-01-01 21:03

I realize that whenever one is dealing with IEEE 754 doubles and floats, some numbers can\'t be represented especially when one tries to represent numbers with lots of digit

相关标签:
1条回答
  • 2021-01-01 21:36

    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.

    0 讨论(0)
提交回复
热议问题