Calculating PI

Proove, that Pi is not 3


	double quarterPI=1.0;
		for (double i=3;i<10000000.0;i+=4)
		{
			quarterPI-=1/i;
			quarterPI+=1/(i+2.0);
		}
		System.out.println("Pi is: "+(quarterPI*4.0));
		
		try
		{
		assert ((quarterPI*4.0) == 3.0);
		}
		catch(java.lang.AssertionError ass)
		{
			System.out.println("Pi is not 3!");
		}

(BTW: assertions need the VM Parameter -ea )


if (Math.PI == 3.0)
{
    System.out.println("PI IS 3.0 YOU KNOW?");
}

Just a joke.

I know your post is a joke, but you’ve committed two grievous floating point errors in only 5 lines of floating point code. You should add a long series of values starting from the smallest (in absolute value); and you should compare two values by checking whether they’re within an appropriate epsilon, not by using ==.

The largest known value of PI is 10 trillion digits.


3.
1415926535 8979323846 2643383279 5028841971 6939937510 : 50
5820974944 5923078164 0628620899 8628034825 3421170679 : 100
2597691971 6538537682 7963082950 0909387733 3987211875 : 4,999,999,999,950
6399906735 0873400641 7497120374 4023826421 9484283852 : 5,000,000,000,000
0874753286 1800417105 0417234177 3440660835 6303291247 : 5,000,000,000,050
0494788787 3350953767 0283816394 7603993291 8259328455 : 5,000,000,000,100
4974817456 4378784878 2784442724 8702748355 5412518205 : 6,000,000,000,000
3820441376 6006093732 4607915193 1761308978 1570965271 : 6,000,000,000,050
1248405812 4259631936 5227923837 0799098542 1979648580 : 7,000,000,000,000
1792125328 5414275137 5682812824 6229793359 0061969684 : 7,000,000,000,050
6636168271 2799313869 3626137973 6405361590 7572836055 : 8,000,000,000,000
6695601471 2802778702 1660409717 9930049024 4234355462 : 8,000,000,000,050
5399802500 0358265067 6788634714 4962622491 5222520345 : 9,000,000,000,000
7793366495 1565215134 1259428770 6232189125 1045517921 : 9,000,000,000,050
9544408882 6291921295 9268257225 1615742394 7483010753 : 9,999,999,999,950
9804871001 5982157822 2070871138 6966940952 1989228675 : 10,000,000,000,000
4392476662 7656619000 2124460557 5531593458 4820611421 : 10,000,000,000,050

better?


	double quarterPI=1.0d;
		for (double i=3.0d;i<10000000.0d;i+=4.0d)
		{
			quarterPI-=1.0d/i;
			quarterPI+=1.0d/(i+2.0d);
		}
		System.out.println("pi: "+(quarterPI*4.0d));
		
		try
		{
		double hereticsDoubt= 0.1d ;
		assert ((quarterPI*4.0d) <= (3.0d+hereticsDoubt) && (quarterPI*4.0d) >= (3.0d-hereticsDoubt));
		}
		catch(java.lang.AssertionError ass)
		{
			System.out.println("pi is not 3!");
		}

No, worse. You’re now asserting a statement which is going to be false whatever the value of quarterPI. I haven’t tested this, but I think I’ve got the loop rework correct.


		double quarterPI = 0;
		for (int i = 9999999; i >= 3; i -= 4)
		{
			quarterPI += 1./(i+2) - 1./i;
		}
		quarterPI += 1;
		System.out.println("pi: "+(quarterPI*4));

		try
		{
			double epsilon = 0.1;
			assert (Math.abs(quarterPI*4 - 3) <= epsilon);
		}
		catch(java.lang.AssertionError ass)
		{
			System.out.println("pi is not 3!");
		}

No the assertion is correct,

I assert that I assume that Pi is between 3.1 and 2.9,

since this assertion (pi==3) is wrong, it disproves the original statment that pi is 3.

You basically just wrote the same in a different manner.

beside: you got lucky that 9999999 works, and “hits” the 3

this 9999998 would not work:

for (int i = 9999998; i >= 3; i -= 4)

gives out:

pi: 3.6137058388800893

this would make it a hard bug to find if you dont know Leibniz’ formula.


AND

my pi is a tiny bit closer to the “Wikipedia pi” than yours ;D

[quote=“Damocles,post:7,topic:45679”]
My bad, I misread it as checking that pi was simultaneously smaller than 2.9 and greater than 3.1.

There was no luck involved in my answer, though. 99 = 3 (mod 4).

A further improvement, which eliminates the potential loss of significance, is to make the loop body quarterPI -= 2./(i*i+2*i);. This requires i to be a double (or long) to avoid overflow.

All I have to say is: WTF???

http://denverlibrary.org/files/nerds254.jpg

calculating

I still prefer spigot algorithms :persecutioncomplex:

Since I don’t care about these things, this isn’t the most up-to-date (I know there’s a better for Pi…but can’t be bothered to look it up)…but see: “ON THE RAPID COMPUTATION OF VARIOUS POLYLOGARITHMIC CONSTANTS”, David Bailey, Peter Borwein and Simon Plouffe.