C ++ POW(2,1000)是normaly以大双,但它的工作。为什么? [英] c++ pow(2,1000) is normaly to big for double, but it's working. why?

查看:162
本文介绍了C ++ POW(2,1000)是normaly以大双,但它的工作。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code:

  #iclude<&math.h中GT;诠释主(){
双somenumber = POW(2,1000);
的printf(%LF \\ n,somenumber);
返回0;
}

我得到这个庞大的数字:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

这显然是要大双。它是如何工作的?


解决方案

  

这显然是要大双。它是如何工作的?


2 1000 是可以重新由双psented $ P $数的范围内。所以,这个数字显然不是太大了一倍。

我怀疑你的过大的意思是,印刷位数的数量大于可存储在双16左右位大得多。有没有错,要求电脑打印超过16个十进制数字。什么是错的是假设这些额外的数字有什么含义。

在该特定情况下,该印刷数是完全正确的。这是因为计算机对待 POW(2 some_int)特别。 2的幂可以重新在双正好psented $ P $。用于计算小数再一个确切的积分值的presentation算法将给出完全正确的十进制再presentation

还有别的,全盘皆输。更改您的程序,以便它打印3 646 例如:

 的#include<&math.h中GT;
#包括LT&;&stdio.h中GT;诠释主(){
  双somenumber = POW(3,646);
  的printf(%LF \\ n,somenumber);
  返回0;
}

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

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?

解决方案

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.

这篇关于C ++ POW(2,1000)是normaly以大双,但它的工作。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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