Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (714) Games in Android Showcase (214) games submitted by our members Games in WIP (787) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Calculating PI  (Read 6025 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 ==.
 Games published by our own members! Check 'em out!
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
 « Reply #9 - Posted 2013-12-17 13:01:29 »

All I have to say is: WTF?
 Games published by our own members! Check 'em out!
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
 « 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

You cannot reply to this message, because it is very, very old.

 Rule (81 views) 2017-03-19 12:43:22 Rule (68 views) 2017-03-19 12:42:17 Rule (77 views) 2017-03-19 12:36:21 theagentd (88 views) 2017-03-16 05:07:07 theagentd (88 views) 2017-03-15 22:37:06 theagentd (84 views) 2017-03-15 22:32:18 theagentd (82 views) 2017-03-15 22:31:11 ral0r2 (137 views) 2017-03-03 11:52:41 ral0r2 (136 views) 2017-03-03 11:42:24 Archive (316 views) 2017-02-27 19:41:49
 basil_ 20x philfrei 15x Apo 11x dime26 10x Archive 9x FrozenShade 9x bornander 7x KaiHH 7x FabulousFellini 7x SteveSmith 7x princec 7x Riven 6x CJC 6x Spasi 6x orangepascal 5x Ecumene 5x
 List 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:05SF/X Librariesby SkyAphid2017-03-02 06:38:56SF/X Librariesby SkyAphid2017-03-02 06:38:32SF/X Librariesby SkyAphid2017-03-02 06:38:05SF/X Librariesby SkyAphid2017-03-02 06:37:51
 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