Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 2767 times)
0 Members and 1 Guest are viewing this topic.
Offline Damocles
« Posted 2013-12-17 08: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 )

Offline SHC
« Reply #1 - Posted 2013-12-17 08: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.

Offline pjt33
« Reply #2 - Posted 2013-12-17 10: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!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-12-17 10: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 : 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

Offline Damocles
« Reply #4 - Posted 2013-12-17 11: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!");
      }

Offline pjt33
« Reply #5 - Posted 2013-12-17 12: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!");
      }
Offline Damocles
« Reply #6 - Posted 2013-12-17 12: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.

Offline Damocles
« Reply #7 - Posted 2013-12-17 12: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 Grin


Offline pjt33
« Reply #8 - Posted 2013-12-17 13: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.
Offline Roquen
« Reply #9 - Posted 2013-12-17 14:01:29 »

All I have to say is: WTF?Huh
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #10 - Posted 2013-12-17 14:05:09 »


calculating

Offline lcass
« Reply #11 - Posted 2013-12-17 23:26:07 »

I still prefer spigot algorithms  persecutioncomplex
Offline Roquen
« Reply #12 - Posted 2013-12-18 10: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (9 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!