c++ pow(2,1000) 对于 double 来说通常太大,但它正在工作。为什么?
代码:
#iclude <math.h>
int main(){
double somenumber = pow(2, 1000);
printf("%lf\n", somenumber);
return 0;
}
我得到这个巨大的数字: 107150860718626732094842504906000181056140481170553360744375038837035105112 4936122493198378815695858127594672917553146825187145285692314043598457757469 857480393456777482423098542107460506237114187795418215304647498358194126739 8767559165543946077062914571196477686542167660429831652624386837205668069376
这对于双倍来说显然太大了。运作如何?
the code:
#iclude <math.h>
int main(){
double somenumber = pow(2, 1000);
printf("%lf\n", somenumber);
return 0;
}
i get this huge number:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
This is obviously to big for double. How it's working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
21000 在双精度数可以表示的数字范围内。所以这个数字对于 double 来说显然并不算太大。
我怀疑你所说的“太大”是指打印的位数远远大于可以存储在双精度数中的16位左右的数字。要求计算机打印 16 位以上的十进制数字并没有什么问题。错误的是假设这些额外的数字有任何意义。
在这种特殊情况下,打印的数字是完全正确的。这是因为计算机对
pow(2,some_int)
进行了特殊处理。 2 的幂可以用 double 精确表示。用于计算精确整数值的十进制表示的算法将给出完全正确的十进制表示。任何其他的,所有的赌注都失败了。更改您的程序,使其打印 3646 例如:
它仍然会打印一个很长的数字,但只有前 16 个左右的数字是正确的。
21000 is within the range of numbers that can be represented by a double. So this number obviously is not too big for a double.
I suspect that what you mean by "too big" is that the number of digits printed is much greater than the 16 or so digits that can be stored in a double. There's nothing wrong with asking a computer to print more than 16 decimal digits. What's wrong is assuming that those extra digits have any meaning.
In this particular case, the printed number is exactly correct. That's because the computer treats
pow(2,some_int)
specially. Powers of 2 can be represented exactly in a double. The algorithm used to compute the decimal representation of an exact integral value will give the exactly correct decimal representation.Anything else, all bets are off. Change your program so it prints 3646 for example:
It will still print a big long number, but only the first 16 or so digits will be correct.
double
通常有 11 位用于 exp(标准化为 -1022~1023),52 位用于事实,1 位用于符号。因此它并不算太大。有关更多说明,请参阅维基百科上的 IEEE 754
double
usually has 11bit for exp (-1022~1023 normalized), 52bit for fact and 1bit for sign. Thus it's simply not too large.For more explanation, see IEEE 754 on Wikipedia
它是 2 的幂,浮点本质上存储为 2 的(倍数)幂。
同样,在十进制系统中,精确表示 101000 只需要很少的空间,这不足为奇,但对于其他值的大幂,例如 3< sup>1000 = 13220708194808066368904552597521443659654220327521481676649 203682268285973467048995407783138506080619639097776968725823 559509545821006189118653427252579536740276202251983208038780 147742289648412743904001175886180411289478156230944380615661 730540866744905061781254803444055470543970388958174653682549 161362208302685637785822902284163983078878969185564040848989 376093732421718463599386955167650189405881090604260896714388 64102814350385648747165832010614366132173102768902855220001。
It is a power of two, and the floating point are essentially stored as (multiples of) powers of two.
Similarly, in decimal system, it shouldn't surprise you that it takes very little room to represent 101000 precisely, but such a concise notation would not be possible for large powers of other values, like 31000 = 1322070819480806636890455259752144365965422032752148167664920368226828597346704899540778313850608061963909777696872582355950954582100618911865342725257953674027620225198320803878014774228964841274390400117588618041128947815623094438061566173054086674490506178125480344405547054397038895817465368254916136220830268563778582290228416398307887896918556404084898937609373242171846359938695516765018940588109060426089671438864102814350385648747165832010614366132173102768902855220001.