Issue
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?
Solution
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
Answered By - Christian Wilkie