cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

while loop conditional test problem

ptc-4449700
1-Newbie

while loop conditional test problem

Hi, I am having a problem getting a while loop conditional test to behave as I expect. See the attached example.

Any ideas what I'm doing wrong?

Thanks, Pete.

1 ACCEPTED SOLUTION

Accepted Solutions

Looks like you are getting rounding errors.

if i =x then it will increment to 0.1 greater , in the case of x=2 if i=2.0000000000000001 then it will return that value.

changing the condition to |i-x| > 10^-5 or similar should help

Regards

Andy

View solution in original post

4 REPLIES 4

Looks like you are getting rounding errors.

if i =x then it will increment to 0.1 greater , in the case of x=2 if i=2.0000000000000001 then it will return that value.

changing the condition to |i-x| > 10^-5 or similar should help

Regards

Andy

Hi Andy, I'd initially thought the same but couldn't understand why it was failing (function_error returning non-zero) on the first loop. But (typically) it was user error. My function last line should return i-1 not i, so that means the function_error return is zero on the first loop, only becoming unpredictable after a number of loops... thus numerical errors it is. Thanks

Pete.

Peter Goodman schrieb:

Hi, I am having a problem getting a while loop conditional test to behave as I expect. See the attached example.

Any ideas what I'm doing wrong?

Thanks, Pete.

You should expect function_error to always return 0.1, because you have used <= instead of <. So when i==x an additional 1/10 is added to i and you function should always return x+0.1.

And even this is not really true - only for values of x which are multiples of 0.1.

Concerning the rest and main part of your question the answer is: roundoff errors by numeric evaluations. You get the correct results by evaluating symbolical. When evaluating numerically set the number of decimal places to the highest possible numer (17) and have a look. Best guess is that numerical evaluation use the "normal" way to deal with numers and that is converting them to binary. 0.1 in binary has an infinite number of decimal point an has to be truncated.

19.11_2.png

By adding 0.1 ten times mathcad got a value slightly under 1 and therefore added an additional 0.1.

by adding 0.1 20 times it got a value slightly over 2 and the while loop stopped (correctly).

You can try working with integers as much as possible

19.11_3.png

But even then you get slight rounding errors if x is not integer.

As long as evaluations are done numerically you will run in such situations. You might try not to ask if a==b but if abs(a-b)<eps where eps is a small value like 10^-6. Or in your case you might ask for i<=x+eps.

WE

Hi Werner,

thanks for your reply. In my problem the steps will be uniform, so I only need to consider values that are multiples of that step. I like your suggestion to use integers to avoid the numerical error, I think I will do that.

Thanks, Pete.

Top Tags