calculating pi

Joined
5/9/06
Messages
296
Points
26
This may seem like an obvious question for those taking c++ but what values/algorithms are you using for pi?
 
I don't really think anybody calculates it, we just use it :)
However, in the very first C++ class we used Monte Carlo to calculate pi.
 
I found an interesting method that uses integrals

where the function is (16*y-16)/(y*y*y*y-2*y*y*y+4*y-4);

and you integrate from 0 to 1 (I used simpsons method simpson(100000000,0.0,1.0))

Are you using the monte-carlo method or a discrete value for pi?

For now I use discrete where pi=3.141592653589793238462643383279502884
 
RussianMike said:
Are you using the monte-carlo method or a discrete value for pi?

For now I use discrete where pi=3.141592653589793238462643383279502884
Nobody uses Monte Carlo to get a value of pi to use in other part of the programs. You can just declare
Code:
long double const PI =3.141592653589793238462643;
. Add the number of decimals place to your heart desire. You can find find the value of pi up to thousands of decimal place online.

Even the M_PI value in the cmath header file has less decimal than the one i just post so you will be fine for the most part. The accuracy lies somewhere else.
 
I don't know if you've done the Monte Carlo bit of your course yet, but I invite you to calculate the number of years your PC would have to run to get Pi to that many decimal places.
Also, as a hint, one mildly common line of questioning at interviews goes something like this:

Here's something to be solved, which numerical methods would you use ?
What are the advantages of your approach with respect to MC ?
Is MC the best scheme ?
How do you choose ?
How do you speed up MC ?
When calculating a price using MC, what do you notice about the Greeks ?
What do you do about that ?
 
In the limit having Pi as the result of an expression may be slightly less efficient.

Recall that a "const" is not the same as "never changes". It is a hint that it won't change much unless you want it to.
Thus the compiler cannot make optimisations that assume it never ever changes.
Constants aren't and variables won't.
http://www.regdeveloper.co.uk/2006/07/26/constants_are_not/
 
Back
Top Bottom