0

I've been wanting to calculate pi with the Gregory-Leibniz series, and there's something wrong with it. I'm a bit of a starter to Java, so this may not be a hard question to answer. I don't know what's going on, so can somebody help me please?

Code so far:

package com.waitdev.pi;

public class Pi {

    static int pi = 0;

    public static void main(String[] args) {

        for (int i = 1; i < 51; i++) {

            if (isOdd(i)) {
                pi = pi+(4/((i*2)-1));
                // System.out.println(i);
            } else {
                pi = pi-(4/(i*2)-1);
                // System.out.println(i);
            }
        }

        System.out.println(pi);
    }

    private static boolean isOdd(int n) {
        if (n % 2 == 1) {
            return true;
        } else {
            return false;
        }
    }
}

FYI: The output for this is 28.

Thanks.

4
  • This is more a math question than a programming question. Commented Mar 18, 2016 at 9:01
  • 8
    How do you intend to write pi into an integer? Commented Mar 18, 2016 at 9:03
  • @Mr.M You're right, I'll try a different type. Thanks for the reminder :) Commented Mar 18, 2016 at 9:06
  • I didn't know that technique, but it's a slow one! The second decimal is guaranteed only from the 1256 iteration... Commented Mar 18, 2016 at 9:29

2 Answers 2

6

You are missing a parenthesis in your else block, and you need to force float calculation by e.g writing 4.0 instead of 4. It should read:

if (isOdd(i)) {
    pi = pi+(4.0/((i*2)-1));
    // System.out.println(i);
} else {
    pi = pi-(4.0/((i*2)-1));
    // System.out.println(i);
}

You also need to use a float or a double for pi.

Extra - as for your isOdd method, it could be simplified like:

private static boolean isOdd(int n) {
     return (n % 2 == 1)
}

But this does not influence the result.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this, and the result is 3.0 now. It's an improvement, but not what I was hoping. Thanks though.
@Waitdev_ 4/((i*2) still give you int value.because int/int=int
You might want to write 4.0 instead of 4 - that'll force the usage of non-integers. Uptaded the answer.
Since using Gregory-Leibniz series could lead to millions of iterations, odness can be established a tad faster by using (i & 0x01) > 0
0

First off: an int is an integer, whole number. you want to change it to:

static double pi = 0;

Otherwise the answer always gets rounded down to a whole number.

Secondly, you are missing parenthesis in your else statement:

} else {
    pi = pi-(4/((i*2)-1));
}

If you want to be completely safe, change your integers in the if and else clause to doubles:

            if (isOdd(i)) {
                pi = pi+(4.0/((i*2.0)-1.0));
                // System.out.println(i);
            } else {
                pi = pi-(4.0/((i*2.0)-1.0));
                // System.out.println(i);
            }

But that shouldn't be neccessary

1 Comment

yes it should. otherwise i = 0-2 will always be pi +/- 1 and 3+ will always be pi +/- 0. Though he only needs one .0, not all of them I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.