0

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.

matrices
  • 33
  • 6
  • 2
    Possible duplicate of [Display first n digits of a number in Java](https://stackoverflow.com/questions/6757708/display-first-n-digits-of-a-number-in-java) – Thomas Weller Sep 04 '17 at 21:59
  • 2
    Are you just asking about printing a given number of decimal places? Or are you asking about the algorithm and how to determine how many iterations are necessary to get the desired number of decimal places correct? As far as I know, no ambitious algorithm should need 426183 iterations to get Pi right to 5 decimal places. – Yunnosch Sep 04 '17 at 22:03
  • Yea i'm trying to print it with just 6 significant figures but java gives 10, Also I need to use delta in order to stop my computation when the delta tells you that you have reached 6 figure precision meaning I need to use the 6 fig version of the variables in my computation rather than the default 10 figure that java gives for numbers of type double – matrices Sep 04 '17 at 23:18
  • You need `DecimalFormat` to obtain the six decimal digits of precision, which you can only get in a decimal radix. The `delta` part of it should be done in `double` or `float`. – user207421 Sep 05 '17 at 00:00
  • And note that this `*1000000/1000000` trick does not work. See my answer [here](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) for why not. – user207421 Sep 05 '17 at 00:29
  • Looking at the series solution will give you an idea. The denominator for the nth term is (2n+1), so the series converges linearly. – duffymo Sep 05 '17 at 00:29

1 Answers1

1

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?

An easy way is to check whether it's within 0.000005 on either side (or whatever other precision you want):

if ((n >= 3.141585) && (n < 3.141595))
Chai T. Rex
  • 2,636
  • 1
  • 12
  • 25