Error in "If 1 Or 0 And 0"

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Error in "If 1 Or 0 And 0"

Post by AZJIO »

There is code that does not work as expected. In the first condition, I get boolean "1 Or 0 And 0" and get output in the debugger. The second example with the same boolean does not output the debugger string.
If I put brackets "(1 Or 0) And 0" in the first expression then it will work as I wanted.

Code: Select all

Structure SelectBall
	x.i
	y.i
	idColor.i
EndStructure

Global SB1.SelectBall
Global SB2.SelectBall

SB1\x = 3
SB1\y = 4

SB2\x = 4
SB2\y = 4

SB1\idColor = 4
SB2\idColor = 4


If ( SB1\y = SB2\y And Abs(SB1\x - SB2\x) = 1 ) Or ( SB1\x = SB2\x And Abs(SB1\y - SB2\y) = 1 ) And SB1\idColor <> SB2\idColor
	Debug Bool( SB1\y = SB2\y And Abs(SB1\x - SB2\x) = 1 )
	Debug Bool( SB1\x = SB2\x And Abs(SB1\y - SB2\y) = 1 )
	Debug Bool( SB1\idColor <> SB2\idColor )
EndIf


If 1 Or 0 And 0
	Debug "100"
EndIf/code]
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Error in "If 1 Or 0 And 0"

Post by idle »

Purebasic uses shortcut evaluation. So it will be true unless you set precedence and use brackets for the statement
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Error in "If 1 Or 0 And 0"

Post by mk-soft »

Code: Select all

a = 1 : b = 0 : c = 0

If a Or b And c
  Debug "true"
Else
  Debug "false"
EndIf

If (a Or b) And c
  Debug "true"
Else
  Debug "false"
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: Error in "If 1 Or 0 And 0"

Post by AZJIO »

mk-soft
If operators have the same precedence (priority), then they are executed sequentially. Hence the first expression is checked "a Or b", then the result is checked with the following "res And c". So both expressions should give the same result.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Error in "If 1 Or 0 And 0"

Post by mk-soft »

Perhaps this is a better way to see how PB shortcut evaluation works.
Var 'a' is non-zero and therefore already true

Code: Select all

a = 1 : b = 0 : c = 0

If a<>0 Or b<>0 And c<>0
  Debug "true"
Else
  Debug "false"
EndIf

If (a<>0 Or b<>0) And c<>0
  Debug "true"
Else
  Debug "false"
EndIf
or

Code: Select all

a = 1 : b = 0 : c = 0

If Bool(a) Or Bool(b) And Bool(c)
  Debug "true"
Else
  Debug "false"
EndIf

If ((a) Or (b)) And (c)
  Debug "true"
Else
  Debug "false"
EndIf
Last edited by mk-soft on Tue Jan 24, 2023 10:15 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Error in "If 1 Or 0 And 0"

Post by STARGÅTE »

AZJIO wrote: Tue Jan 24, 2023 9:06 pm Hence the first expression is checked "a Or b", then the result is checked with the following "res And c". So both expressions should give the same result.
Not. That's not how it works in PureBasic.
In the first example "a Or b And c", the first check is, if "a" is #True. If "a" is #True, the whole evaluation stops and the result is #True. There is no check of the "And"
In the second example "(a Or b) And c", the first check is again if "a" is #True, and it stops. But then, also the "#True And c" is checked, which is #False.

However, there seems to be a bug, when using first AND and then OR:

Code: Select all

a = 1 : b = 1 : c = 0

If a And b Or c
  Debug "true"
Else
  Debug "false"
EndIf

If (a And b) Or c
  Debug "true"
Else
  Debug "false"
EndIf

If a And (b Or c)
  Debug "true"
Else
  Debug "false"
EndIf
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Error in "If 1 Or 0 And 0"

Post by mk-soft »

Code: Select all

If a And b Or c
If forward logic ... in ASM and C backend

If a = true -> ok -> next
and
If b = true -> ok -> next
or
If c = true -> no -> false

Even though it is strange, it is somehow correct
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Error in "If 1 Or 0 And 0"

Post by idle »

Code: Select all

a = 1 : b = 1 : c = 0

If a And b Or c
  Debug "true"
Else
  Debug "false"
EndIf

If (a And b) Or c
  Debug "true"
Else
  Debug "false"
EndIf

If a And (b Or c)
  Debug "true"
Else
  Debug "false"
EndIf
what compiler version are you using I get with PB 6.01b
true
true
true
and with 6
false
true
true
and now I'm confused with who's on 1st base?
https://youtu.be/bpxkyTc9Z38?t=68
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Error in "If 1 Or 0 And 0"

Post by juergenkulow »

Code: Select all

;Truth table
Debug "a And b Or c"
Debug "a b c "
For a=0 To 1
  For b=0 To 1
    For c=0 To 1      
      If a And b Or c
        Debug Str(a)+" "+Str(b)+" "+Str(c)+" true"
      Else
        Debug Str(a)+" "+Str(b)+" "+Str(c)+" false"
      EndIf
    Next 
  Next
Next 

Debug "(a And b) Or c"
Debug "a b c "
For a=0 To 1
  For b=0 To 1
    For c=0 To 1      
      If (a And b) Or c
        Debug Str(a)+" "+Str(b)+" "+Str(c)+" true"
      Else
        Debug Str(a)+" "+Str(b)+" "+Str(c)+" false"
      EndIf
    Next 
  Next
Next 

Debug "a And (b Or c)"
Debug "a b c "
For a=0 To 1
  For b=0 To 1
    For c=0 To 1      
      If a And (b Or c)
        Debug Str(a)+" "+Str(b)+" "+Str(c)+" true"
      Else
        Debug Str(a)+" "+Str(b)+" "+Str(c)+" false"
      EndIf
    Next 
  Next
Next 

; 6.01 Beta 1 C Linux x64
; a And b Or c
; a b c 
; 0 0 0 false
; 0 0 1 true
; 0 1 0 false
; 0 1 1 true
; 1 0 0 false
; 1 0 1 true
; 1 1 0 true
; 1 1 1 true
; (a And b) Or c
; a b c 
; 0 0 0 false
; 0 0 1 true
; 0 1 0 false
; 0 1 1 true
; 1 0 0 false
; 1 0 1 true
; 1 1 0 true
; 1 1 1 true
; a And (b Or c)
; a b c 
; 0 0 0 false
; 0 0 1 false
; 0 1 0 false
; 0 1 1 false
; 1 0 0 false
; 1 0 1 true
; 1 1 0 true
; 1 1 1 true
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Error in "If 1 Or 0 And 0"

Post by STARGÅTE »

@idle: You are right. It was fixed in 6.01 Beta 1. I was using 6.00 LTS.
Also the first example in this thread "1 Or 0 And 0" gives now #False because the evaluation is:
1 Or 0 -> 1
1 And 0 -> 0
instead of:
1 Or 0 -> 1 , stop and return 1

However, shortcut evaluation is still possible within all True-Or-Statements or False-And-Statements:

Code: Select all

Procedure MyPrint(X.i, Bool.i)
	Debug X
	ProcedureReturn Bool
EndProcedure

If MyPrint(1, #True) Or MyPrint(2, #True) Or MyPrint(3, #True)
	Debug "True"
Else
	Debug "False"
EndIf

If MyPrint(1, #False) And MyPrint(2, #False) And MyPrint(3, #False)
	Debug "True"
Else
	Debug "False"
EndIf

If MyPrint(1, #False) And MyPrint(2, #False) Or MyPrint(3, #True)
	Debug "True"
Else
	Debug "False"
EndIf

If MyPrint(1, #True) Or MyPrint(2, #False) And MyPrint(3, #True)
	Debug "True"
Else
	Debug "False"
EndIf
1
True
1
False
1
3
True
1
3
True
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Error in "If 1 Or 0 And 0"

Post by ChrisR »

idle wrote: Wed Jan 25, 2023 12:13 am what compiler version are you using I get with PB 6.01b
...
and now I'm confused with who's on 1st base?
https://youtu.be/bpxkyTc9Z38?t=68
:lol: Funny, I don't know, fortunately I usually use parenthesis.

I'm a little confused by StarGate's reading of "1 Or 0 And 0", I get the same result (and fixed in 6.01) but I'm not sure I read it the same way.
I read it naturally as learned at school, following the operators priority: Not > And > Or and same for bitwise: "~" > "&" > "|", like in SQL, C, C++,...
1 Or 0 And 0 -> 1 Or (0 And 0) -> 1 Or 0 -> 1
1 And 0 Or 0 -> (1 And 0) Or 0 -> 0 Or 0 -> 0
Contrary to the help where they have the same priority level :!:

Is it possible to have a confirmation of how it is really treated, left to right, priority ?
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Error in "If 1 Or 0 And 0"

Post by juergenkulow »

Code: Select all

; six=one+two+three
  MOV    r15,qword [v_one]
  ADD    r15,qword [v_two]
  ADD    r15,qword [v_three]
  MOV    qword [v_six],r15
PB Documentation Variables Operators priorities
Add note: Operators of the same priority are processed from left to right.
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Error in "If 1 Or 0 And 0"

Post by ChrisR »

juergenkulow wrote: Wed Jan 25, 2023 6:26 pm Add note: Operators of the same priority are processed from left to right.
Thanks, I do well to put brackets because it is not the way I read it naturally and used in RPG, SQL or in most languages.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Error in "If 1 Or 0 And 0"

Post by idle »

ChrisR wrote: Wed Jan 25, 2023 4:45 pm
idle wrote: Wed Jan 25, 2023 12:13 am what compiler version are you using I get with PB 6.01b
...
and now I'm confused with who's on 1st base?
https://youtu.be/bpxkyTc9Z38?t=68
:lol: Funny, I don't know, fortunately I usually use parenthesis.

I'm a little confused by StarGate's reading of "1 Or 0 And 0", I get the same result (and fixed in 6.01) but I'm not sure I read it the same way.
I read it naturally as learned at school, following the operators priority: Not > And > Or and same for bitwise: "~" > "&" > "|", like in SQL, C, C++,...
1 Or 0 And 0 -> 1 Or (0 And 0) -> 1 Or 0 -> 1
1 And 0 Or 0 -> (1 And 0) Or 0 -> 0 Or 0 -> 0
Contrary to the help where they have the same priority level :!:

Is it possible to have a confirmation of how it is really treated, left to right, priority ?
I use parenthesis until I go cross eyed.
Post Reply