Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculating PI  (Read 9182 times) 0 Members and 1 Guest are viewing this topic.
Damocles
 « Posted 2013-12-17 07:11:21 »

Proove, that Pi is not 3

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `   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 )
SHC
 « Reply #1 - Posted 2013-12-17 07:36:20 »

Proove, that Pi is not 3

 1  2  3  4 `if (Math.PI == 3.0){    System.out.println("PI IS 3.0 YOU KNOW?");}`

Just a joke.

pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #2 - Posted 2013-12-17 09:23:17 »

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 ==.
SHC
 « Reply #3 - Posted 2013-12-17 09:55:44 »

The largest known value of PI is 10 trillion digits.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `3.1415926535 8979323846 2643383279 5028841971 6939937510 : 505820974944 5923078164 0628620899 8628034825 3421170679 : 1002597691971 6538537682 7963082950 0909387733 3987211875 : 4,999,999,999,9506399906735 0873400641 7497120374 4023826421 9484283852 : 5,000,000,000,0000874753286 1800417105 0417234177 3440660835 6303291247 : 5,000,000,000,0500494788787 3350953767 0283816394 7603993291 8259328455 : 5,000,000,000,1004974817456 4378784878 2784442724 8702748355 5412518205 : 6,000,000,000,0003820441376 6006093732 4607915193 1761308978 1570965271 : 6,000,000,000,0501248405812 4259631936 5227923837 0799098542 1979648580 : 7,000,000,000,0001792125328 5414275137 5682812824 6229793359 0061969684 : 7,000,000,000,0506636168271 2799313869 3626137973 6405361590 7572836055 : 8,000,000,000,0006695601471 2802778702 1660409717 9930049024 4234355462 : 8,000,000,000,0505399802500 0358265067 6788634714 4962622491 5222520345 : 9,000,000,000,0007793366495 1565215134 1259428770 6232189125 1045517921 : 9,000,000,000,0509544408882 6291921295 9268257225 1615742394 7483010753 : 9,999,999,999,9509804871001 5982157822 2070871138 6966940952 1989228675 : 10,000,000,000,0004392476662 7656619000 2124460557 5531593458 4820611421 : 10,000,000,000,050`

Damocles
 « Reply #4 - Posted 2013-12-17 10:51:25 »

better?

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `   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!");      }`
pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #5 - Posted 2013-12-17 11:43:18 »

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.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `      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!");      }`
Damocles
 « Reply #6 - Posted 2013-12-17 11:54:42 »

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.
Damocles
 « Reply #7 - Posted 2013-12-17 11:58:04 »

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

pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #8 - Posted 2013-12-17 12:57:23 »

I assert that I assume that Pi is between 3.1 and 2.9,
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.
Roquen

JGO Kernel

Medals: 517

 « Reply #9 - Posted 2013-12-17 13:01:29 »

All I have to say is: WTF?
Damocles
 « Reply #10 - Posted 2013-12-17 13:05:09 »

calculating
lcass
 « Reply #11 - Posted 2013-12-17 22:26:07 »

I still prefer spigot algorithms
Roquen

JGO Kernel

Medals: 517

 « Reply #12 - Posted 2013-12-18 09:30:32 »

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.
Pages: [1]
 ignore  |  Print

 EgonOlsen (78 views) 2018-06-10 19:43:48 EgonOlsen (58 views) 2018-06-10 19:43:44 EgonOlsen (78 views) 2018-06-10 19:43:20 DesertCoockie (260 views) 2018-05-13 18:23:11 nelsongames (158 views) 2018-04-24 18:15:36 nelsongames (157 views) 2018-04-24 18:14:32 ivj94 (898 views) 2018-03-24 14:47:39 ivj94 (162 views) 2018-03-24 14:46:31 ivj94 (811 views) 2018-03-24 14:43:53 Solater (175 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org