the code:
#iclude
int main(){
double somenumber = pow(2, 1000);
printf(\"%lf\\n\", somenumber);
return 0;
}
i get this huge
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
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.
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.