I have to calculate Pi using leibinz series
pi/4 = 1 - 1/3 + 1/5 - 1/7 ...
I need the number of iterations it takes to get to six significant digit accuracy, originally java has 15 digits beyond the decimal place but I only need 5(3.14159)in precision and I cannot round, How would I check sig figure precision without rounding tho and without using the Math,round function if java is gonna give me 9 digits beyond the decimal place?
public class Main {
public static void main(String[] args) {
double series = 0;
double denominator = 1;
double numerator = 1;
double testingPi;
double formattedTestingPi = 0;
double formattedMathPi = Math.round(Math.PI * 100000.0) / 100000.0;
int max = 1200000;
int iterations = 0;
double formattedPreviousPi = 0;
double formattedDelta = 0;
for(int i = 1; i < max;i++)
{
iterations = i;
if((i % 2) != 0)
{
series = series + (numerator/denominator);
}
else if((i % 2) == 0)
{
series = series + ((numerator/denominator) * -1);
}
testingPi = series * 4;
formattedTestingPi = (Math.round(testingPi * 100000.0))/100000.0;
formattedDelta = formattedTestingPi - formattedPreviousPi;
if(Math.abs(formattedDelta) <= 0.000009)
{
i = max;
}
formattedPreviousPi = formattedTestingPi;
denominator = denominator + 2;
}
System.out.println("Formatted Delta :" + formattedDelta);
System.out.println("Iterations :" + iterations);
System.out.println("Pi Formatted Computed :" + formattedTestingPi);
System.out.println("Pi Formatted Math Library :" + formattedMathPi);
}
}
The output is
Formatted Delta :0.0
Iterations :426183
Pi Formatted Computed :3.14159
Pi Formatted Math Library :3.14159
I do not need the solution but some help as to how could this be done without rounding.