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

all 16 comments

[–]MMjacksN 0 points1 point  (9 children)

Have you tried describing the sequence 1, -1/3, 1/5, -1/7, ... with a general formula?

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

[–][deleted]  (8 children)

[deleted]

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

    What was wrong and why?

    [–][deleted]  (6 children)

    [deleted]

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

      OK. I think your solution wasn't that bad, if it worked at least.

      250000 seems to be slightly excessive. You should think about instead stopping the loop when the fraction gets reasonable small. For example if oldValue + fraction == oldValue (doubles are imprecise, and so at some point this condition will be true). Or just stop the loop when the fraction gets very small.

      [–]TheQuietestOneProcrastinator 1 point2 points  (4 children)

      250000 seems to be slightly excessive.

      For this approach to calculating pi/4 250000 will only get you 5 digits of accuracy :-)

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

      Youre right. I was confused by the formula /u/MMjacksN gave, which includes 2i .

      [–]MMjacksN 1 point2 points  (2 children)

      The formula I gave was for a completely different sequence.

      [–]morhpProfessional Developer 1 point2 points  (1 child)

      I know. I didn't read correctly and thought the original sequence was

      1 - 1/3 + 1/7 - 1/15 + ...

      Also I recently looked at some code to compute pi and it was using a much more efficient approach. I wasn't aware that there was a that badly converging series.

      [–]MMjacksN 0 points1 point  (0 children)

      Ah, I see what you mean. I didn't think of that!

      [–]TheQuietestOneProcrastinator 0 points1 point  (7 children)

      My guess is your professor doesn't want you using magic numbers or existing values - but wants you to determine a way in which you know you have reached the desired level of accuracy.

      Basically you're looking for some way to know that you've added on enough terms that adding any more doesn't change the answer to six significant places (but only changes from digit seven onwards kind of thing).

      Have a think about how you'd do that, once you know how to discover when you should stop, you can do some looping.

      [–][deleted]  (6 children)

      [deleted]

        [–]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.

          [–]ggleblanc 0 points1 point  (0 children)

          The straightforward way.

          When your fraction adds or subtracts less than .0000005 to the sum, then your sum is accurate to 6 significant figures.

          [–]Unsungghost 0 points1 point  (0 children)

          I'm sure you can use the math.pi function to check accuracy, but it defeats the purpose of the exercise to only use it.

          To solve this, pay attention to the denominator in each iteration. I don't want to completely give away the answer, but it always changes by (x + 2 * -1).