How to increase the accuracy of pow in C ++ for large numbers (10 ^ 19)?

I expect to calculate 9^19 . my code is:

  cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout<<pow(9,19)<<endl; 

As a result, the last two digits are 0 : 1350851717672992000 . In Python 9**19 I got 1350851717672992089L . Seems to be a floating point problem. How to increase the accuracy of pow ? or how to convert higher precision than pow ?

I am compiling with gcc version 4.8.2.

+5
source share
2 answers

This is really a floating point problem: a typical 64-bit double gives only 53 bits or about 15 decimal digits of precision.

You could (or could not) get more precision from a long double . Or, for integers up to 10 19 you can use uint64_t . Otherwise, there is no standard type with greater accuracy: you will need a library, such as GMP or Boost.Multiprecision .

+9
source

The precision of double is about 16 decimal digits.

This means that only the first 16 decimal digits are accurate, the remaining digits are as good as noise.

+1
source

All Articles