# Thread: Value of Pie Problem

1. ## Value of Pie Problem

I want this to display upto 100 decimal places what can I do.

Code:
```#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout <<768 * sqrt(2-sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+1)))))))));

return 0;
}```
I want this to display upto 100 decimal places what can I do

2. Look up setprecision.

3. Originally Posted by http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_ computers
Double precision, called "double" in the C language family, and "double precision" or "real*8" in Fortran. This is a binary format that occupies 64 bits (8 bytes) and its significand has a precision of 53 bits (about 16 decimal digits).
You'll need to use an arbitrary precision math library.

4. I need some 31 digits and the above code turned wrong and the right thing to use that i found was
4*(4*arctan(1/5)-arctan(1/239))

but how can you use this arctan function in c++ what is the header file?

and also I used the set precision command but what data type to take I only got up to 16 decimal places using long

5. > I only got up to 16 decimal places using long
Hm... I wonder why...

> but how can you use this arctan function in c++ what is the header file?
cmath would be my first guess.

To get the value of pi to desired decimal places you need to consider two things. One is you need to use more of the half angle formula and at the same time you need to change the multiplier(which on your code is set to 768) to different figure. The other thing is as you use more of the half angle formula you need to consider your data type. The more you use half angle formula the more square root operation you do and consequently you loose precession to a considerable extent.

It's better you change your formula or use an arbitrary precision math library as suggested above. The library helps you to display the value you calculated to the desired decimal places.

Refer the documenation for arctan function on http://www.cplusplus.com/reference/clibrary/cmath/atan/

By the way, it's an interesting piece of code!

7. Originally Posted by iC++
By the way, it's an interesting piece of code!
That had to be implemented using a loop or a recursive function.

8. The simplest equation you can use with any floating point type:

Code:
```template < typename Float >
Float pi( void )
{
static Float const
value = Float( 4 ) * atan( Float( 1 ) );
return value;
}```

9. Please can anybody teach me in baby steps on arbitrary precision math library
i do not need the files but just the name of the header file and syntax of the function to be used.

Please can anybody teach me in baby steps on arbitrary precision math library
i do not need the files but just the name of the header file and syntax of the function to be used.
Such libraries are non-standard, so it really depends on which one you choose to use (and consequently, you do need the files ). For example, you could use the GMP, possibly with the C++ interface.

11. Do this in hex and you're gold:

http://en.wikipedia.org/wiki/BBP_alg...thm_for_.CF.80

BBP Formula -- from Wolfram MathWorld

Digit-Extraction Algorithm -- from Wolfram MathWorld

Of course, since you need (only) 100 places, brute-force calculation with a high-precision library is the way to go. But if you ever need only the millionth decimal, you know what to do.

12. Originally Posted by siavoshkc
That had to be implemented using a loop or a recursive function.
I was referring to the idea of calculating the value of pi using a C++ code.

13. If you enter 'setprecision(25)', it gives you a lot of trailing zeroes. I think that 100 is simply too big.
Regardless, here's the code (It gives you a blank screen, but if you make the number smaller, it'll work):

Code:
```#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{

cout << fixed << showpoint
<< setprecision(100)
<< 768 * sqrt(2-sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+1)))))))));

system ("pause");
return 0;
}```

14. ## Why does this display zero any idea?

Code:
```#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
int n;
cout << setprecision (100) << (12*atan(1/49) + 32*atan(1/57) - 5*atan(1/239) + 12*atan(1/110443))*4;
cin >> n;
return 0;
}```

15. You are using integer divisions. In the world of integers 1 / 49 = 0, 1 / 57 = 0 etc.

You have to use double literals in these calculations (at least one) to make the compiler treat this a floating point division: 1.0 / 49, 1.0 / 57

However, no matter how elaborate the formula, and no matter how large values you give to setprecision, a double only has precision worth of 16-17 decimal digits. Therefore, it is impossible to represent pi with higher precision using the double data type. The setprecision() function only tells cout how to display the number, it does not affect the properties of a double data type.