Calculating Pi in C#

Started by
18 comments, last by Mercury 17 years, 4 months ago
Quote:Original post by Demosthenes
Why not Math.PI? :D
Because they want arbitrary precision
Math.PI is a double. What if I want to print out 1 million digits of pi?

Using atan isn't going to help anyone (as already pointed out) because now you're going to need another routine to calculate atan of a number up to an arbitrary precision.
Advertisement
Quote:Because they want arbitrary precision
Math.PI is a double. What if I want to print out 1 million digits of pi?

Using atan isn't going to help anyone (as already pointed out) because now you're going to need another routine to calculate atan of a number up to an arbitrary precision.


It was a joke following Eelco's post, hence the smiley next to my original post. Oh, nevermind.
By the way, anyone know a formula for calculating e?

Chris 'coldacid' Charabaruk – Programmer, game designer, writer | twitter

Dave math pages
Quote:Original post by coldacid
By the way, anyone know a formula for calculating e?


I just use the general power series for e^x and simplify it to the case when x=1 which gives you the sum from n = 0 to infinity 1/n!. I unroll the loop based on how precise I want the approximation for optimization. It's the simplest way, and it converges quickly anyway.
Quote:Original post by Eelco
Quote:Original post by Beer Hunter
Quote:Original post by Programmer One
pi / 4 = 4 * tan-1(1 / 5) - tan-1(1 / 239)
Anything wrong with pi = 4 * tan-1(1)?
well yeah, it seems to me that if you want to write a software program to approximate an infinite series, any use of hardware implemented trig functions (which are also based on series approximation) kindof defeats the point.
I know, I was just pointing out that it appears preferable to that other formula.
Mathworld on pi formulas, or for the faster stuff: pi iterations. For example:
x = sqrt(2);y = sqrt(x);pi = 2 + sqrt(2);while (not enough):    sx = sqrt(x)    x2 = (sx + 1/sx)) / 2;    y2 = (y * sx + 1/sx) / (y + 1)    x = x2;    y = y2    pi = pi * (x + 1) / (y + 1)

This method probably converges too fast to benchmark (unless you have arbitrary precission numbers of course).

Or you could try:
while (having fun):   pi = pi + sin(pi)
some pi code i found in python (prints an infinite number of pi digits)

import sysdef main():    k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L    while 1:        # Next approximation        p, q, k = k*k, 2L*k+1L, k+1L        a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1        # Print common digits        d, d1 = a/b, a1/b1        while d == d1:            output(d)            a, a1 = 10L*(a%b), 10L*(a1%b1)            d, d1 = a/b, a1/b1def output(d):    # Use write() to avoid spaces between the digits    # Use str() to avoid the 'L'    sys.stdout.write(str(d))    # Flush so the output is seen immediately    sys.stdout.flush()if __name__ == "__main__":    main()
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
The fastest digit extraction algorithm for pi that I know of is an improvement of the BBP algorithm by Fabrice Bellard.
Quote:Original post by avidlinuxuser
Quote:Original post by coldacid
By the way, anyone know a formula for calculating e?


I just use the general power series for e^x and simplify it to the case when x=1 which gives you the sum from n = 0 to infinity 1/n!. I unroll the loop based on how precise I want the approximation for optimization. It's the simplest way, and it converges quickly anyway.

Another way to do it without using series' is to just evaluate it as a limit.

e = lim x->infinity (1+ 1/x)^x

So, as x gets larger, the whole thing gets closer to e. For x=999999999, e is approximately 2.7182818270999043210175031107634.

This topic is closed to new replies.

Advertisement