Andorino
Andorino

Reputation: 43

What is wrong with my pi calculator?

I'm using Wallis' method to calculate pi, and I think I did it right. At least I thought I did anyway. I think the problem (output is 0)has to do with rounding and remainders, though I can't be sure. Here's the code:

import java.util.Scanner;

public class WallisPi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int a = 2;
        int b = 3;
        int c = 1;
        int pi = 0;
        double acc = 0.0;

        int n = scan.nextInt();
        scan.close();

        for (int i = 0; i <= n; i++) {
            pi = (2 / 3) * c;
            if (a > b) {
                b += 2;
            } else {
                a += 2;
            }
            c = a / b;
        }

        pi *= 4;

        System.out.println("The approximation of pi is " + pi + ".");
        acc = Math.PI - pi;
        System.out.println("It is " + acc + " off.");
    }
}

Since posting this I've made some changes to the code, though it's still not quite functional. I get 2.666..., so there's something else at work here as well.

import java.util.Scanner;

public class WallisPi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        double a = 2.0;
        double b = 3.0;
        double c = 1.0;
        double pi = 0;
        double acc = 0.0;

        int n = scan.nextInt();
        scan.close();

        for (int i = 0; i <= n; i++) {
            pi = (2.0 / 3.0) * c;
            if (a > b) {
                b += 2;
            } else {
                a += 2;
            }
            c = a / b;
        }

        pi *= 4;

        System.out.println("The approximation of pi is " + pi + ".");
        acc = Math.PI - pi;
        System.out.println("It is " + acc + " off.");
    }
}

Upvotes: 2

Views: 175

Answers (2)

laune
laune

Reputation: 31290

int a=2;
int b=3;
double pi=2;
for(int i=0;i<=n;i++){
   pi *= (double)a/(double)b;
  if(a>b){
    b+=2;
  } else {
    a+=2;
  }
}
pi*=2;

Using n = 4000 yields 3.141200


Here's the whole program, fixed:

import java.util.Scanner;

public class WallisPi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();

        double pi = 2;
        int a = 2;
        int b = 3;

        for (int i = 0; i <= n; i++){
            pi *= (double) a / (double) b;
            if (a > b) {
                b += 2;
            } else {
                a += 2;
            }
        }

        pi *= 2;

        double acc = Math.PI - pi;

        System.out.println("The approximation of pi is " + pi + ".");
        System.out.println("It is " + acc + " off.");
    }
}

Upvotes: 6

Mureinik
Mureinik

Reputation: 311403

Since your varibles are ints, all your divisions are integer divisions, omitting the fraction (and preserving only the whole part of the result). For accurate results, you should define your variables as doubles:

double a=2;
double b=3;
double c=1;
double pi=0;

Upvotes: 0

Related Questions