5

10^1.64605 = 44.2639330165

However in C++ using pow:

double p = pow(10,1.64605) returns 44.2641. 

Is there a way to increase the precision here? I tried casting both sides to long double but that didn't help either.

More interesting is:

cout<<p;
double a = -1.64605;
cout<<pow(10,-a);
p = pow(10, (-p));
cout<<p;

the output is:

-1.64605
44.2639
44.2641

Why?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
user3639557
  • 4,791
  • 6
  • 30
  • 55
  • 2
    Other than using a Bignum lib, probably there isn´t something to make it better. – deviantfan Apr 22 '15 at 03:36
  • when getting the 44.2641 what are you using to display it? debugger? cout? printf? – diverscuba23 Apr 22 '15 at 03:56
  • 1
    try adding a #include and then cout << setprecision(10) << p and cout << setprecision(10) << pow(10 -a) – diverscuba23 Apr 22 '15 at 03:58
  • which compiler/os are you using? I end up with 44.26393302 when using the setprecision call on the output. – diverscuba23 Apr 22 '15 at 04:06
  • It's just a precision problem. `pow(10, 1.646052)` is 44.2641. – Mark Ransom Apr 22 '15 at 05:12
  • 1
    Doing: printf("%.12f\n",pow(10,1.64605)); I get: 44.263933016536 – Petr Vepřek Apr 22 '15 at 05:17
  • It's just an issue with formatting. I checked with Mathematica and my GCC gives the exact same output as Mathematica, and Mathematica has arbitrary precision. What exactly is the use case? If you had a real use case it would be easier to help. Precision with `pow` can sometimes be improved if you can rearrange the expression to use base 2 etc. – Skurmedel Apr 22 '15 at 05:25
  • We can't see where you initialised p. Or what type it is. Do beware that 1.64605 is not exactly representable. – David Heffernan Apr 22 '15 at 05:43

2 Answers2

7

cout is truncating your double for display, but the value calculated by pow is probably at least as precise as you expect. For how to get more precision displayed in the console see:

How do I print a double value with full precision using cout?

I'll elaborate given the prodding by David.

You stated that double p = pow(10,1.64605) returns 44.2641 but this is incorrect. It returns 44.26393301653639156; without any formatting specifiers this displays as 44.2639 (as you see later).

When you cout the original value of p in the second code snippit it displays -1.64605 (due to reduced precision formatting) and you are assuming that it is exactly -1.64605 whereas it is actually somewhere between -1.64605115... and -1.64605213..., which does evaluate to 44.2641 in the expression cout << pow(10, (-p));

Community
  • 1
  • 1
Qartar
  • 400
  • 1
  • 9
  • 1
    This scratches at the surface but doesn't really get to the bottom of the issue. For instance see for yourself what `cout << pow(10,1.64605)` outputs even with full precision. – David Heffernan Apr 22 '15 at 06:41
  • I'm not sure what you're getting at. If you're talking about `44.2641` he obviously just mixed up the results of his program when he posted. – Qartar Apr 22 '15 at 15:45
  • No he did not. The explanation is exactly as described in my answer. – David Heffernan Apr 22 '15 at 15:46
  • I mean that he wrote `44.2641` was the result of his original calculation when it was actually the result of his second. – Qartar Apr 22 '15 at 15:51
  • That's wrong. `pow(10,1.64605)` returns `44.2639`. It's `pow(10, (-p))` that returns `44.2641`. Which begs the question as to what value is `p`. It's all explained in my answer. – David Heffernan Apr 22 '15 at 15:53
  • OK, now your answer is accurate. The voting is very odd, but there you go! ;-) – David Heffernan Apr 22 '15 at 19:46
1

The answer will be found by examining p. You did not show how it was initialized.

You will find that p != a, even though they appear to be the same when you print them to the console. When you printed to the console you only printed the first 6 significant decimal digits. Print both values to greater precision and you will see that they are not equal.

You said that:

double p = pow(10,1.64605);

evaluates to 44.2641. But that is not true. If you actually execute that code you will see that.

double p = pow(10,1.64605);
cout << p;

outputs 44.2639.

Your code differs slightly from that above. It is:

cout << p;
p = pow(10, (-p));
cout << p;

Output the original value of p to full precision and all will be revealed.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490