Reputation: 69
my output is coming wrong. I guess i'm wrong with casting. please help me out.
int n;
cin>>n;
unsigned long long int a,s;
cin>>a;
s=(2*pow(10,n)+a);
But when I am giving large n
like 17
or 18
then my output which is s
is not coming as expected.
see image for output
e.g: when n=17
, a=67576676767676788
then s=267576676767676800
which ideally should be 2*10^17 + 67576676767676788
Upvotes: 1
Views: 815
Reputation: 37512
First you have to understand what is going on.
double
and returned value is a double
too.double
has 16 significant digits (in decimal representation).double
to long long int
is silently performedunsigned long long int
- if this type has 64 bits the maximum power of 10 is 19Now if you want to exceed this limitation you should use an external library. gmp
is quite nice.
If it is acceptable to have a limitation from range of unsigned long long int
just implement your own power function.
Upvotes: 8