在双倍的最小/最大范围内有双数不能表示的整数吗? [英] Are there any whole numbers which the double cannot represent within the MIN/MAX range of a double?

查看:179
本文介绍了在双倍的最小/最大范围内有双数不能表示的整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到,无论什么时候处理IEEE 754双打和浮点数,有些数字不能表示,特别是当一个数字在小数点后面用很多数字表示时。这是很好理解,但我很好奇,如果在双(或浮点数)的MIN / MAX范围内有任何无法表示的整数,因此需要四舍五入到最接近的可表示的IEEE 754表示?



例如,非常大的数字有时以双精度或浮点数表示,即使它们是整数。显然,使用一个int64或者一些这样的大整数数据类型会更好,但是人们仍然每隔一段时间就使用大数字双打。



是否有可以调用的数字你可以给我一个数学的理由,为什么它不会是一个问题?

解决方案

当然,那里是不能表示为双精度浮点的整数。



所有整数不超过 Pow(2,53) 9007199254740992 可表示的。从 Pow(2,53) Pow(2,54)(即 18014398509481984 ),只有偶数是可表示的。奇数将被舍入。



当然,它会继续如此。从 Pow(2,54) Pow(2,55)只有4的整数倍4分)是可以表示的,从 Pow(2,55) Pow(2,56)只有8的倍数,等等。



这是因为双击精确的浮点格式有53位(二进制数字)作为尾数(significantand)。

很容易验证我的说法。例如,将 10000000000000001 号码作为 integer64 。将它转换为 double ,然后返回到 integer64 。你会看到精度的损失。



当你输入非常大的双精度数字时,整数的百分比肯定是可以代表的。例如,在 1E + 300 (介于 Pow(2,996) Pow> (2,997))我们正在谈话的倍数 Pow(2,944) 1.4870169084777831E + 284 )。这与 double 精确到16个十进制数字的事实是一致的。所以300个数字的整数只会被记住到第一个数字。 16个数字(实际上是53个二进制数字)。



另外:十个不能精确表示的第一个幂是 1E + 23 (或者100个sextillion,小规模的命名风格)。在这个数字附近,只有 16777216 (即 Pow(2,24))的整数倍是可表示的,到23日的权力显然不是二十四日的权力的倍数。因子分解是 10 ** 23 == 2 ** 23 * 5 ** 23 ,所以我们可以平均分配23次,而不是24次。

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 digits after the decimal point. This is well understood but I was curious if there were any whole numbers within the MIN/MAX range of a double (or float) that couldn't be represented and thus needed to be rounded to the nearest representable IEEE 754 representation?

For instance very large numbers are sometimes represented in doubles or floats even if they are whole numbers. Clearly using a straight up int64 or some such large integer datatype would be better but people still use doubles for large numbers every so often.

Are there any numbers that can be called out as non-representable or can you give me a mathematical reason why it wouldn't be a problem?

解决方案

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.

这篇关于在双倍的最小/最大范围内有双数不能表示的整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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