Results 1 to 5 of 5

Thread: Question about calculating pi

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I wanted to have more presicion than just using Math.PI, but i can't figure out how to do it (partially). Someone else hepled me get started, but their code didn't work entirely, as I just keep getting the same two numbers. This si what I have:

    Code:
    package piApplet;
    
    public class piApplet
    {
      public static void main(String[] args)
      {
        long factorial = 1;
        int i = 1;
        double errMax = 1E-8, piOld = 1.0, piNew = 1.0, error = 1.0;
    
        while (error > errMax)
        {
          factorial = factorial * i;
          piNew = piOld + (1.0 / (double)factorial) * Math.pow(-1, i);
          error = Math.abs(piNew - piOld);
          i++;
          piOld = piNew;
          System.out.println(piOld);
        }
    
        System.out.println("pi = " +piNew);
      }
    }
    Can anyone help?

  2. #2
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Maybe you should do this genius:

    Code:
    package pi;
    public class pi
    {
      public static void main(String[] args)
      {
        long denom = 1;
        int i = 1;
        double errMax = 1E-5, piOld = 4.0, piNew = 4.0, error = 1.0;
    
        while (error > errMax)
        {
          denom = denom + 2;
          piNew = piOld + (4.0/(double)denom) * Math.pow(-1,i);
          error = Math.abs(piNew - piOld);
          i++;
          piOld = piNew;
        }
    
        System.out.println(piNew);
      }
    }

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Talking

    Maybe I should, who knows?
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    That code looks fimiluar don't it?

  5. #5

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    i need an icon.
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width