简体   繁体   中英

Math line only running once in a loop

this is my first interaction with this site, I have heard good things and I hope I can find the answer that I am looking for. I am learning Java and using the Eclipse IDE in a computer science class at my high school and I came across a problem that neither my teacher or I can solve. Here are the instructions.

"The German Mathematician GottfriedLeibniz developed the follow method to approximate the value of π.

π/4 = 1 - 1/3 + 1/5 - 1/7 + ...

Write a program that allows the user to specify the number if iterations used in this approximation and displays the resulting value."

Now the code.

import java.util.Scanner;

public class GottfriedLeibnizPi {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        System.out.print("Enter how many iterations you want to go to: ");
        int iterations = reader.nextInt();
        double pi = 0;

        if (iterations >= 0) {
            for (int count = 0; count <= iterations - 1; count++) {
                System.out.println("pi: " + pi + " count: " + count);        //debug statement to show pi and count before running the code
                if (count % 2 == 0) {
                    pi = pi + (1 / (1 + (2 * count)));        // first to run. starts at pi + 1 and every other loop adds 1/(1+2n)
                } else {
                    pi = pi - (1 / (1 + (2 * count)));        // first to run. starts at pi - 1/3 and every other loop subtracts 1/(1+2n)
                }
                System.out.println("pi: " + pi + " count: " + count + "\n");        //debug statement to show pi and count after running the code
            }
            pi = pi * 4;        //obtains the true value of pi
            System.out.println("The value of pi after " + iterations + " iterations is " + pi);
        } else {
            System.out.println("Please enter a non-negative number");
        }
    }
}

Here is the output with the the debugging statements if I enter five at the prompt.

Enter how many iterations you want to go to: 5
pi: 0.0 count: 0
pi: 1.0 count: 0

pi: 1.0 count: 1
pi: 1.0 count: 1

pi: 1.0 count: 2
pi: 1.0 count: 2

pi: 1.0 count: 3
pi: 1.0 count: 3

pi: 1.0 count: 4
pi: 1.0 count: 4

The value of pi after 5 iterations is 4.0

My math says that answer should be 3.3396... but the math in my loop does not appear to run more than once. I have not found anything on here that is close to my problem, does anyone know what is wrong?

The problem seems to be a roundoff caused by integer division. Try replacing the relevant part of your code with this:

if(count % 2 == 0){
    pi = pi + (1 / (1 + (2.0 * count)));        // first to run. starts at pi + 1 and every other loop adds 1/(1+2n)
}

else{
    pi = pi - (1 / (1 + (2.0 * count)));        // first to run. starts at pi - 1/3 and every other loop subtracts 1/(1+2n)
}

Since "1" in the numerator is an integer, and "(1 + (2*Count))" in the denominator is also an integer, you will get integer division which truncates any remainder from the division. Since the denominator in this case is going to be greater than or equal to 1, 1/(positive integer greater than 1) will always result in 0 in integer division. By adding a decimal to one of the integers Java will treat it as a double and not perform integer division.

So actually, there is no problem with the execution of the lines. When I run the code with the above changes I get the same answer as given by your teacher.

The value of pi after 5 iterations is 3.3396825396825403

int/int gives an int type, by which you loose the fractional part of the divison ( 3/2 gives 1 ) . So you need -

pi = pi + (1.0 / (1.0 + (2 * count)));    // Notice the 1.0   

Similarly in the other case too.

In both the cases of:

pi = pi + (1 / (1 + (2 * count))); // (1 / (1 + (2 * count))) everything here is an int so, so you are losing . and everything after . 

change it to

pi = pi + (1d / (1 + (2 * count))); 
iterations=int(input('enter no of iterations'))
adding = 0
for i in range(1, iterations + 1, 2):
    if i % 4 == 1:
        adding += 1/i
    else:
        adding -= 1/i 
 pi = 4 * adding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM