0

I'm trying to make a program that calculates PI using the following algorithm:

PI = 4 x (1 - 1/3 + 1/5 - 1/7 + 1/9 ....) etc.

I don't understand how the operator placement is affecting the variable.

The outcome produced on the first iteration of the loop makes sense, but then it seems to reset to initialised value and ignore the assignment operators the second time through. Repeating this outcome 1, then outcome 2, then outcome 1, outcome 2, and so forth...

I tried looking in this book called "Java: How to Program (Early Objects), 11th Edition" and in chapter four they went over operators but I could see that they didn't cover the conditions within a loop.

   double k = 1.0, j = 1.0;

   double sum = 0, PI = 0;

   while((Math.abs(PI-Math.PI)>0.000001)){
       sum += k/j;

       j = j + 2;

       k=-k;

       PI = 4 * sum;    

       System.out.println(k);
    }   

I changed the conditions of the while loop to run 4 times and print k. I expected the first printing of the variable 'k' to be -1.0. It is, but the second printing of k (second looping of while loop) is 1.0. The third is -1.0, then 4th is 1.0, and so forth...

I don't understand why it isn't -1.0 on all iterations, because with java assignment operators, as far as I know, if the left operator is '=' and the right operator is an incremental or decremental symbol, then the result should always be that the variable k will always = -k.

2
  • 2
    What happens if k == -1.0 and you're doing -k on it? Or more precisely, what is -(-1.0)? Commented Jul 23, 2019 at 6:17
  • You don't subtract a fraction in every iteration, but in every second one. Setting k = -k in every iteration just switches the sign. Commented Jul 23, 2019 at 6:21

3 Answers 3

3

You initialize k outside the loop, so that only happens once. The initial value is 1.

During each iteration you negate k:

k=-k;
  • During the first iteration 1 is negated to become -1.
  • During the second iteration -1 is negated to become 1.
  • During the third iteration 1 is negated to become -1.
  • and so on
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I appreciate the time taken to help me =]
2

Before loop

k = 1

  • loop #1 k = -(1.0) : so now k = -1.0, it's carried -1.0 over to the next loop.

  • loop #2 k = -(-1.0) : so now k = 1.0, it's carried 1.0 over to the next loop.

  • loop #3 k = -(1.0) : so now k = -1.0, it's carried -1.0 over to the next loop.

  • and so on

k is changing every loop, just like how j is not 3.0 every loop.

The incremental and decremental symbol you're talking about is probably k-- and k++, or maybe k-=k and k+=k. I'm not sure.

1 Comment

Ah, perfect. Thank you for this. I had a feeling it was to do with when two '-' signs are together, it equals a positive. I just was getting confused in my head. But you explained it perfectly. Thank you.
2

as far as I know, if the left operator is '=' and the right operator is an incremental or decremental symbol, then the result should always be that the variable k will always = -k.

I think you are confusing the operation -= with the operation =-

a -= b perform a-b and store the result in a

a = -b perform -b (change sign) and then store -b into a

That is what that k = -k does. Take the value of k, change its sign and store into k. That is equivalent to say that change the sign of k.

1 Comment

Thank you very much, I appreciate the time taken to help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.