I have to compute pi to six decimal precision using the leibniz series and I managed to do it except the assigment has some restrictions.
pi / 4 = 1 -1/3 + 1/5 - 1/7 ....
" cannot utilize the math library π in your computation - direct or indirect. Use ONLY the delta (difference) of your running computation to determine when to stop your loop "
I do not understand what this means by difference of your running computation. Another way I was told was "When your computed value ceases to change then your loop can stop"
package com.company;
public class Main {
public static void main(String[] args) {
double series = 0;
double denominator = 1;
double numerator = 1;
double pi;
double testingPi;
double formattedTestingPi = 0;
double formattedMathPi = Math.round(Math.PI * 1000000.0) / 1000000.0;
int max = 1200000;
int iterations = 0;
for(int i = 1; i < max;i++)
{
if((i % 2) != 0)
{
series = series + (numerator/denominator);
}
else if((i % 2) == 0)
{
series = series + ((numerator/denominator) * -1);
}
denominator = denominator + 2;
testingPi = series * 4;
formattedTestingPi = (Math.round(testingPi * 1000000.0))/1000000.0;
if( formattedTestingPi == formattedMathPi)
{
iterations = i;
i = max;
System.out.println("We stop");
}
}
pi = series * 4;
System.out.println("Number of Iterations :" + iterations);
System.out.println("Unformatted Series :" + series);
System.out.println("Unformatted Math Library PI:" + Math.PI);
System.out.println("Unformatted Computed PI:" + pi);
System.out.println("Formatted Computed PI:" + formattedTestingPi);
System.out.println("Formatted Math Library PI:" + formattedMathPi);
}
}
I do not want the solution to the assignment, I just want to know what does delta of computation mean and how is it different what I'm doing right now?
Output
Number of Iterations :1181461
Unformatted Series :0.7853983749998679
Unformatted Math Library PI:3.141592653589793
Unformatted Computed PI:3.1415934999994715
Formatted Computed PI:3.141593
Formatted Math Library PI:3.141593