c++ pow(2,1000) 对于 double 来说通常太大,但它正在工作。为什么?

发布于 2024-12-03 22:08:32 字数 500 浏览 1 评论 0原文

代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-12-10 22:08:32

这对于双倍来说显然太大了。它是如何工作的?

21000 在双精度数可以表示的数字范围内。所以这个数字对于 double 来说显然并不算太大。

我怀疑你所说的“太大”是指打印的位数远远大于可以存储在双精度数中的16位左右的数字。要求计算机打印 16 位以上的十进制数字并没有什么问题。错误的是假设这些额外的数字有任何意义。

在这种特殊情况下,打印的数字是完全正确的。这是因为计算机对 pow(2,some_int) 进行了特殊处理。 2 的幂可以用 double 精确表示。用于计算精确整数值的十进制表示的算法将给出完全正确的十进制表示。

任何其他的,所有的赌注都失败了。更改您的程序,使其打印 3646 例如:

#include <math.h>
#include <stdio.h>

int main(){
  double somenumber = pow(3, 646);
  printf("%lf\n", somenumber);
  return 0;
}

它仍然会打印一个很长的数字,但只有前 16 个左右的数字是正确的。

This is obviously to big for double. How it's working?

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:

#include <math.h>
#include <stdio.h>

int main(){
  double somenumber = pow(3, 646);
  printf("%lf\n", somenumber);
  return 0;
}

It will still print a big long number, but only the first 16 or so digits will be correct.

一抹微笑 2024-12-10 22:08:32

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

呆萌少年 2024-12-10 22:08:32

它是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文