4

I am looking to calculate 9^19. my code is:

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

The result has the last 2 digits equal to 0: 1350851717672992000. In Python,9**19 got me 1350851717672992089L . Seems a floating point issue. How could I raise the precision for pow? or how to preform a better precision power than pow?

I am compiling with gcc version 4.8.2.

Deanie
  • 2,316
  • 2
  • 19
  • 35
Assem
  • 11,574
  • 5
  • 59
  • 97

2 Answers2

9

It is indeed a floating-point issue: a typical 64-bit double only gives 53 bits, or about 15 decimal digits, of precision.

You might (or might not) get more precision from long double. Or, for integers up to about 1019, you could use uint64_t. Otherwise, there's no standard type with more precision: you'll need a library such as GMP or Boost.Multiprecision.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

The precision of double is around 16 decimal digits.

That means, that only the first 16 decimal digits are exact, the rest of the digits are as good as noise.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271