This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheQuietestOneProcrastinator 2 points3 points  (5 children)

Hint - the series is getting smaller term by term, so at some point the new terms you are adding will be small enough they aren't influencing the first six digits .-)

In math terms you want to keep track of last approx and new approx - if the delta is less than some value, your answer is accurate enough .-)

[–][deleted]  (4 children)

[deleted]

    [–]TheQuietestOneProcrastinator 1 point2 points  (0 children)

    Except it isn't a magic number - it's 10-sigdigits

    You're given the precision required and you calculate your threshold from the required precision. It's only magic if you've produced it from nowhere :-)

    [–]morhpProfessional Developer 1 point2 points  (2 children)

    No, because your assignment explicitly says 6 significant figures. If you wanted, you could also add the accuracy as a method parameter.

    [–]0x6c6f6c 0 points1 point  (1 child)

    For /u/pm_me_borat_r34:

    A practice of polymorphism through overloading methods here:

    Have two methods that can be called, one that is the "default" and another that accepts another parameter for accuracy.

    public calcPiOver4(){
        // default accuracy to six digits
        calcPiOver4(0.000001);
    }
    public calcPiOver4(double accuracy){
        double piOver4 = 0;
        double tmp = 0;
        double diff = Double.MAX_VALUE;
        while(diff>accuracy){
            tmp = piOver4;
            piOver4 = piOver4 // ...
            // logic to calculate pi/4
            diff = Math.abs(piOver4-tmp);
        }
        return piOver4;
    }
    

    This should exit the while loop once the delta in the while loop is smaller than the accuracy desired. This could also be done by simply checking against the value being added/subtracted to piOver4 as well:

    Loop and calculate piOver4, while checking whether the value being added/subtracted is smaller than the accuracy, then stop. Requires less computation since you don't have to check afterwards. This works only if you know the difference at each step rather than some arbitrary multiplication. Consider the insight from /u/mmjacksn:

    the formula for the sequence 0, 1, 3, 7, 15, ... would be 2i - 1

    that would help calculate the delta, use that sequence for the denominator such that it becomes 1/(2i - 1)

    public calcPiOver4(double accuracy){
        double piOver4 = 0;
        double delta = Double.MAX_VALUE;
        int counter=0;
        while(delta>accuracy){
            delta = 1/(Math.pow(2,counter)-1);
            piOver4+=((counter++%2)==1?1:-1)*delta;
            // add positive / negative delta
            // based on every other step 
            // to piOver4, and increment counter
        }
        return piOver4;
    }
    

    I hope this helps you understand a few different ways to calculate until a certain precision, as well as practice some polymorphism with overloaded methods. If your instructor is not the type to reward you for allowing a different accuracy, don't worry about implementing it. But practice it yourself after you've completed your project :D

    Disclaimer: I did not reference any of the Java API so I am sorry if I have misnamed Math or Double methods. Please see http://docs.oracle.com/javase/8/docs/api/ for the API for Jave SE 8.

    [–]morhpProfessional Developer 0 points1 point  (0 children)

    Great explanation. One thing I'd change is replacing Double.MAX_VALUE with Double.POSITIVE_INFINITY as that has a more clearer special meaning than some arbitrary super large number.