Page 1 of 1

IF THEN 'true' bug

Posted: Mon Aug 30, 2021 12:03 pm
by Electroguard
This tricky bug only shows up when evaluating two values (eg: b+c) 'true' that are not caged by parenthesis, else it hides unnoticed, giving false confidence to other code, such as the following snippet, which appears to run ok...

Code: [Local Link Removed for Guests]

a=11: b=2: c=3
if a < b+c then wlog "a<" 
if a < b+c then wlog "a<" else wlog "a>"
But both lines 2 and 3 have a bug, even though they are not failing because the IF evaluates as false, despite the ELSE of line 3 obviously being true.
Changing the value of 'a' from 11 to 1 will cause lines 2 and 3 to fail because the IF condition will then evaluate to true.

The bug can be avoided by using parenthesis, eg the following line will be ok whether it evaluates to true or false...

Code: [Local Link Removed for Guests]

if a < (b+c) then wlog "ok" else wlog "ok"

Re: IF THEN 'true' bug

Posted: Mon Aug 30, 2021 3:44 pm
by cicciocb
Yes, comparison operators should be given lower priority than arithmetic operators.

Using the parenthesis will avoid any misinterpretation.

Re: IF THEN 'true' bug

Posted: Mon Aug 30, 2021 5:41 pm
by Fernando Perez
Let's not forget the case of the power operator, which should precede the multiplication operator, but it is not.

Code: [Local Link Removed for Guests]

wlog "Without parentheses"
for i = 0 to 4
  x = 35 * 2 ^ i
  wlog x
next i  
wlog

wlog "Reverse order"
for i = 0 to 4
  x = 2 ^ i * 35
  wlog x
next i  
wlog

wlog "With parentheses"
for i = 0 to 4
  x = 35 * (2 ^ i)
  wlog x
next i

image.png

Re: IF THEN 'true' bug

Posted: Mon Aug 30, 2021 7:03 pm
by Jan Volk
First you do what is in parentheses;
Then Powers/Roots from left to right;
Then Multiply/Divide from left to right;
Finally Add/Subtract from left to right.

Re: IF THEN 'true' bug

Posted: Mon Aug 30, 2021 8:51 pm
by Fernando Perez
It seems from my code that Annex gives priority to the first operator it finds by traversing the instruction from left to right, regardless of whether it is power or multiply.
When in doubt, I always use the parenthesis system.
But if I'm right, it would be convenient to correct it in the help manual.