In a program i have to calculate pow(2,N) where 0<N<=1000. which data type is suitable for this?
I have used unsigned long long int but its range is also limited.
In a program i have to calculate pow(2,N) where 0<N<=1000. which data type is suitable for this?
I have used unsigned long long int but its range is also limited.
the function pow() returns double. So you can use double to hold the value.
using namespace std;
int main()
{
double i;
i = pow(2,1000);
cout<<i<<endl; //prints 1.07151e+301
i = i*4;
cout<<i<<endl; //prints 4.28603e+301
i = pow(i,((double)1/1002));
cout<<i<<endl; //prints 2
return 0;
}
the function pow() returns double. So you can use double to hold the value.
using namespace std; int main() { double i; i = pow(2,1000); cout<<i<<endl; //prints 1.07151e+301 i = i*4; cout<<i<<endl; //prints 4.28603e+301 i = pow(i,((double)1/1002)); cout<<i<<endl; //prints 2 return 0; }
this is the c forum use printf() instead of cout
the answer should not be in exponential form.
this is the c forum use printf() instead of cout
oops, i forgot that !!!
@himanshu : if you want to print the full number then try printing like this
printf("%f",i);
it is not showing the correct result. i mean the printf("%f",i) is valid upto number i=56 which has 17 digits in its answer but for number exceeding 56 its shows 17 significant digits and all other digits are zeroes.
for eg
your code will give "144115188075855870" for 57 but the exact answer is "144115188075855872"...
I don't understand why it happens to you. But here I copy paste my code and output. I use gcc to compile. Not sure whether the compiler is causing the difference in my case. But here it goes.
janus ~/tools $ cat power.c
#include <stdio.h>
#include <math.h>
int main()
{
double i = pow(2,1000);
printf("%f\n",i);
return 0;
}
janus ~/tools $ gcc power.c
janus ~/tools $ ./a.out
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376.000000
janus ~/tools $
In order to verify it's only a printing problem in your case, do an inverse power on the value that you get and see if you get exactly 2 as the answer. I have showed it in my previous post.
Moreover, see this link. Maybe it'll help you in case of printing issues.
http://byuh.doncolton.com/courses/tut/printf.pdf