为什么double可以存储比unsigned long long更大的数字? [英] Why double can store bigger numbers than unsigned long long?

查看:3052
本文介绍了为什么double可以存储比unsigned long long更大的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,我不明白为什么double可以存储比unsigned long long更大的数字。由于它们都是8字节长,所以64位。

在无符号long long中,所有64位用于存储一个值,另一方面,double有1表示符号,11表示指数,52表示尾数。即使用于尾数的52位将被用来存储没有浮点的十进制数,它仍然有63位...



但是LLONG_MAX是明显小于DBL_MAX ...

为什么?

解决方案

原因是无符号long long 会存储精确整数,而 double 存储尾数有限的52位精度)和一个指数。

这允许 double 存储非常大的数字(大约10 < sup> 308 ),但不完全一样。你在一个 double 中有大约15个(近16个)有效的十进制数字,其余的308个可能的小数是零(实际上是未定义的,但是你可以假定为零更好的理解)。
一个无符号long long 只有19个数字,但是每一个数字都被精确定义。



编辑:

回复下面的评论这个确实有效 ,指数为11位,尾数为52位。尾数在起始处有一个隐含的1位,不存储,所以有效地有53个尾数位。 2 53 是9.007E15,所以你有15,几乎16位的十进制数字来处理。

指数有一个符号位,范围可以从-1022到+1023,用于缩放(二进制左移或右移)尾数(2 1023约为10 307),因此是范围上的限制),所以非常小且非常大的数字同样可以用这种格式。

但是,当然,所有你可以表示的数字只有精确到适合matissa。



<总而言之,浮点数不是很直观,因为简单的十进制数不一定代表浮点数。这是由于尾数是二元的事实。例如,有可能(也很容易)用完美的精度来表示任何高达几十亿的正整数,或者数字如0.5或0.25或0.0125。另一方面,也有可能代表一个像10 250 的数字,但只是近似的。事实上,你会发现10 <250>和<250> +1是相同的数字(wait,what ???)。这是因为尽管你可以很容易地有250位数字,你没有那么多显着的数字(读显着为已知或已定义)。

另外,代表0.3这样看起来很简单的东西也是可能的,尽管0.3甚至不是大的数字。然而,你不能用二进制表示0.3,不管你附加了什么样的二进制指数,你都不会找到任何精确到0.3的二进制数(但你可以非常接近)。

有些特殊值被保留为无穷大(包括正数和负数)以及不是数字,因此您的总数要少于总数理论范围。另一方面,

无符号long long 并不以任何方式解释位模式。您可以表示的所有数字只是位模式表示的确切数字。每个数字的每个数字都是精确定义的,不会发生缩放。


The question is, I don't quite get why double can store bigger numbers than unsigned long long. Since both of them are 8 bytes long, so 64 bits.

Where in unsigned long long, all 64 bits are used in order to store a value, on the other hand double has 1 for sign, 11 for exponent and 52 for mantissa. Even if 52 bits, which are used for mantissa, will be used in order to store decimal numbers without floating point, it still has 63 bits ...

BUT LLONG_MAX is significantly smaller than DBL_MAX ...

Why?

解决方案

The reason is that unsigned long long will store exact integers whereas double stores a mantissa (with limited 52-bit precision) and an exponent.

This allows double to store very large numbers (around 10308) but not exactly. You have about 15 (almost 16) valid decimal digits in a double, and the rest of the 308 possible decimals are zeroes (actually undefined, but you can assume "zero" for better understanding).
An unsigned long long only has 19 digits, but every single of them is exactly defined.

EDIT:
In reply to below comment "how does this exactly work", you have 1 bit for the sign, 11 bits for the exponent, and 52 bits for the mantissa. The mantissa has an implied "1" bit at the beginning, which is not stored, so effectively you have 53 mantissa bits. 253 is 9.007E15, so you have 15, almost 16 decimal digits to work with.
The exponent has a sign bit, and can range from -1022 to +1023, which is used to scale (binary shift left or right) the mantissa (21023 is around 10307, hence the limits on range), so very small and very large numbers are equally possible with this format.
But, of course, all numbers that you can represent only have as much precision as will fit into the matissa.

All in all, floating point numbers are not very intuitive, since "easy" decimal numbers are not necessarily representable as floating point numbers at all. This is due to the fact that the mantissa is binary. For example, it is possible (and easy) to represent any positive integer up to a few billion, or numbers like 0.5 or 0.25 or 0.0125, with perfect precision.
On the other hand, it is also possible to represent a number like 10250, but only approximately. In fact, you will find that 10250 and 10250+1 are the same number (wait, what???). That is because although you can easily have 250 digits, you do not have that many significant digits (read "significant" as "known" or "defined").
Also, representing something seemingly simple like 0.3 is also only possible approximately, even though 0.3 isn't even a "big" number. However, you can't represent 0.3 in binary, and no matter what binary exponent you attach to it, you will not find any binary number that results in exactly 0.3 (but you can get very close).

Some "special values" are reserved for "infinity" (both positive and negative) as well as "not a number", so you have very slightly less than the total theoretical range.

unsigned long long on the other hand, does not interprete the bit pattern in any way. All numbers that you can represent are simply the exact number that is represented by the bit pattern. Every digit of every number is exactly defined, no scaling happens.

这篇关于为什么double可以存储比unsigned long long更大的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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