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?
The reason for it not working is that pow(2,100) is too big a number to fit in any primitive data type supported. (viz int, long , double).
Given two numbers base and exponent, pow() function finds x raised to the power of y i.e. xy. Basically in C exponent value is calculated using the pow() function. pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function.
pow() function returns the base to the exponent power, as in base exponent , the base and the exponent are in decimal numeral system.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With