It is currently Mon Jul 22, 2019 12:59 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: [4.31] Boolean operators are right associative
PostPosted: Tue Jun 23, 2009 10:37 am 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7439
Location: Norway
Code:
a = 0
b = 1
c = 1
d = 1

If a And b Or (c And d)
  Debug "true"
Else
  Debug "false" ; Wrong!
EndIf


Code:
If a And b Or (c And d)

; is compiled as

If a And (b Or (c And d))

; but should be

If (a And b) Or (c And d)



Top
 Profile  
Reply with quote  
 Post subject: Re: [4.31] Boolean operators are right associative
PostPosted: Tue Jun 23, 2009 6:41 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3560
Location: Utah, USA
Trond wrote:
Code:
If a And b Or (c And d)

; is compiled as

If a And (b Or (c And d))

; but should be

If (a And b) Or (c And d)



Your interpretation of the effective compilation of the expressions doesn't hold up. Here's a visual of what I mean:

Code:
If 0 And 0 Or (0 And 0) = 0    ===    If 0 And (0 Or (0 And 0)) = 0
If 0 And 0 Or (0 And 1) = 0    ===    If 0 And (0 Or (0 And 1)) = 0
If 0 And 0 Or (1 And 0) = 0    ===    If 0 And (0 Or (1 And 0)) = 0
If 0 And 0 Or (1 And 1) = 0    ===    If 0 And (0 Or (1 And 1)) = 0
If 0 And 1 Or (0 And 0) = 0    ===    If 0 And (1 Or (0 And 0)) = 0
If 0 And 1 Or (0 And 1) = 0    ===    If 0 And (1 Or (0 And 1)) = 0
If 0 And 1 Or (1 And 0) = 0    ===    If 0 And (1 Or (1 And 0)) = 0
If 0 And 1 Or (1 And 1) = 0    ===    If 0 And (1 Or (1 And 1)) = 0
If 1 And 0 Or (0 And 0) = 0    ===    If 1 And (0 Or (0 And 0)) = 0
If 1 And 0 Or (0 And 1) = 0    ===    If 1 And (0 Or (0 And 1)) = 0
If 1 And 0 Or (1 And 0) = 0    ===    If 1 And (0 Or (1 And 0)) = 0
If 1 And 0 Or (1 And 1) = 0     <>    If 1 And (0 Or (1 And 1)) = 1
If 1 And 1 Or (0 And 0) = 0     <>    If 1 And (1 Or (0 And 0)) = 1
If 1 And 1 Or (0 And 1) = 0     <>    If 1 And (1 Or (0 And 1)) = 1
If 1 And 1 Or (1 And 0) = 0     <>    If 1 And (1 Or (1 And 0)) = 1
If 1 And 1 Or (1 And 1) = 1    ===    If 1 And (1 Or (1 And 1)) = 1


The expressions should have identical evaluations if what you say is true, but they don't.

As to how they should be evaluated I am not expressing an opinion at the moment. I have found nothing in the documentation that prescribes the manner in which they should be evaluated, so there's no definitive right or wrong for me to compare it to. Are you saying that the evaluation is inconsistent or unpredictable? If so that would be a bug. I couldn't confirm it from the code you posted.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 23, 2009 6:58 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 19, 2006 1:57 pm
Posts: 4828
Location: Germany
I remember languages where connecting of two or more operators without brackets spawned an error-message...

since it's always a good idea to bracket such expressions, why not add a compiler warning for cases like this?


... this suggestion is not relativating the observation made by Trond and Demivec ...

_________________
oh... and have a nice day.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 23, 2009 7:52 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Jun 12, 2007 10:30 am
Posts: 617
Location: not there...
Kaeru Gaman wrote:
since it's always a good idea to bracket such expressions
Yes, because I think, without brackets there`s no evaluation after If a -> "Short-circuit evaluation"?
freak wrote:
If the second part got evaluated always...
from http://www.purebasic.fr/english/viewtop ... 787#275787

_________________
5.70


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 23, 2009 8:27 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3560
Location: Utah, USA
As a followup to my previous post, it would appear that there is a more serious problem.

In my previous post I included results that were clearly incorrect:
Code:
If 1 And 0 Or (1 And 1) = 0
If 1 And 1 Or (0 And 0) = 0
If 1 And 1 Or (0 And 1) = 0
If 1 And 1 Or (1 And 0) = 0

This was the display of expressions that were evaluated with variables.


When the same expressions are evaluated with constants the results are correct. Here's code to demonstrate:
Code:
;evaluate if using constants
Macro cDoIf(a,b,c,d)
  If a And b Or (c And d)
    Debug "true"
  Else
    Debug "false"
  EndIf
EndMacro

;evaluate if using variables
Procedure vDoIf(a,b,c,d)
  cDoIf(a,b,c,d)
  Debug ""
EndProcedure

cDoIf(1,0,1,1) ;displays true
vDoIf(1,0,1,1) ;displays false which is wrong

cDoIf(1,1,0,0) ;displays true
vDoIf(1,1,0,0) ;displays false which is wrong

cDoIf(1,1,0,1) ;displays true
vDoIf(1,1,0,1) ;displays false which is wrong

cDoIf(1,1,1,0) ;displays true
vDoIf(1,1,1,0) ;displays false which is wrong


@Edit: removed a duplicate test from code and shorten code so that the conditions in the If/Else/Endif would only have to be changed in one placed for other tests.

_________________
Image


Last edited by Demivec on Tue Jun 23, 2009 11:54 pm, edited 4 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [4.31] Boolean operators are right associative
PostPosted: Tue Jun 23, 2009 8:27 pm 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7439
Location: Norway
Demivec wrote:
Trond wrote:
Code:
If a And b Or (c And d)

; is compiled as

If a And (b Or (c And d))

; but should be

If (a And b) Or (c And d)



Your interpretation of the effective compilation of the expressions doesn't hold up.
This whole thing is very strange. But first of all, you used integer literals, while I used variables. Do not assume they work the same. In fact they don't. The expression I reported as a bug works correctly with literal numbers:

Code:
a = 1
b = 1
c = 0
d = 1

If 1 And 1 Or (0 And 1)
  Debug "Literals"
EndIf

If a And b Or (c And d)
  Debug "Variables"
EndIf


To get everything right, I read the assembly output instead of reading the results, it turns out my initial prediction of how it was compiled was wrong, however, it is definetely not compiled correctly. The resulting assembly code makes no sense.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 23, 2009 9:23 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8004
Location: Fort Nelson, BC, Canada
If it's a bug it's never going to affect me. I wouldn't dream of writing code like that.
Code:
If (a And b) Or (c And d)
  Debug "true"
Else
  Debug "false" ; Wrong!
EndIf

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 23, 2009 9:30 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 6061
Code:
If 1 And 1 Or (0 And 1)
  Debug "Literals"
EndIf

If a And b Or (c And d)
  Debug "Variables"
EndIf


Ouch. Perhaps a bug but definitely ugly code!

Always make sure to use brackets to identify 'evaluation levels'...

What is it you intended?

if (1 and 1) or (0 and 1)

or did you intend

if 1 and ( 1 or ( 0 and 1 ))

?

It's a bad programming habit to write code that depends on the language specific interpretation if there is no need to do so. It's even worse to write code that the human eye would have trouble to identiy. At least, that's what I think :-)

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 23, 2009 10:00 pm 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7439
Location: Norway
Quote:
It's even worse to write code that the human eye would have trouble to identiy.
Which is why I wrote it without brackets...

I don't understand how anyone could have problems reading this code.

Just as you'd write this:
a = 1 + 2 - (1 + 3)

And never this:
a = (1 + 2) - (1 + 3)

It's not depending on special language features at all. It expects simple left-to-right evaluation, common to absolutely all programming languages except the crazy ones like lisp and APL.
It is my opinion that using parentheses to specify an existing precedence is horrendous because the only real use for parenthesis is precedence override. So you're actually SCREAMING PRECEDENCE OVERRIDE!, and then it turns out there is no such thing in the code. It's deliberately misleading, and I will not do it in such simple cases.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 23, 2009 10:07 pm 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7439
Location: Norway
This code gives the wrong result as well:
Code:
a = 0
b = 1
c = 0
d = 1

If (a And b) Or c And d
  Debug "true"
Else
  Debug "false" ; Wrong!
EndIf


It should be like this:

0. (a And b) Or c And d
Collapse "a And b" into False since a is false
1. False Or c And d
Collapse "False Or c" into False since c is false
2. False And d
The result of the evaluation is false since "False And something" is false.

However, PB says the result is true! It is impossible. "False and true" cannot become true. Only if d is set to 0 as well does the expression evaluate to false.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jun 24, 2009 1:36 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4571
Location: Spa, relaxing and thinking, and learning...
Trond wrote:
Code:
a = 0
b = 1
c = 0
d = 1

If (a And b) Or c And d
  Debug "true"
Else
  Debug "false" ; Wrong!
EndIf
True!
There is a buggy output :o

_________________
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend


Top
 Profile  
Reply with quote  
 Post subject: Re: [4.31] Boolean operators are right associative
PostPosted: Fri Mar 15, 2019 12:27 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4571
Location: Spa, relaxing and thinking, and learning...
Someone can explain that behaviour still in PB5.70?
That is scaring :!:

_________________
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye