IF THEN 'true' bug

Here we can discuss about the problem found
Post Reply
User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

IF THEN 'true' bug

Post 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"
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: IF THEN 'true' bug

Post by cicciocb »

Yes, comparison operators should be given lower priority than arithmetic operators.

Using the parenthesis will avoid any misinterpretation.
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: IF THEN 'true' bug

Post 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
You do not have the required permissions to view the files attached to this post.
Jan Volk
Posts: 71
Joined: Wed Mar 03, 2021 1:35 pm
Been thanked: 23 times

Re: IF THEN 'true' bug

Post 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.
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: IF THEN 'true' bug

Post 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.
Post Reply