His goal is to stop looping once either of the numbers is over 20% larger than the other.
abs(B-A)/B <= 0.2
^ This part is true as long as A is not at least 21% larger than B.
abs(A-B)/A <= 0.2
^ This part is true as long as B is not at least 21% larger than A.
while(abs(B-A)/B <= 0.2 || abs(A-B)/A <= 0.2);
^ This means as long as (A is not at least 21% larger than B) or (B is not at least 21% larger than A), then keep looping.
So let's say that
A == 100 and
B == 121. Just from looking at it we know that B is 21% larger than A.
abs(B-A)/B is
abs(121 - 100) / 121 which is 0.17, smaller than 0.2, making
abs(B-A)/B <= 0.2 be true.
abs(A-B)/A is
abs(100 - 121) / 100 which is 0.21, greater than 0.2, making
abs(B-A)/B <= 0.2 be false.
The while statement is
while (true || false), which is the same as
while (true).
| @fg109 I just tried that and it produced "PCT2: 20.0 24.0" instead of "PCT2: PCT2: 33.0 51.0" when I entered "20.0 24.0 33 51". |
That's because floating point numbers aren't 100% accurate. You might try to divide 1.0 by 4.0 and get 0.249999999. You can try using
double instead of
float variables and see if that helps.