c++ pow(2,1000) is normaly to big for double, but it's working. why?

后端 未结 3 1283
小蘑菇
小蘑菇 2020-12-17 10:25

the code:

#iclude 

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

i get this huge

3条回答
  •  清歌不尽
    2020-12-17 11:15

    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.

提交回复
热议问题