Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Playing with the mouse
#11
(12-11-2022, 05:39 PM)TempodiBasic Wrote: wow I  googled and found the order of execution of the logical operator in QB
from first to last: NOT AND OR XOR EQV IMP


is this the same order of execution made by QB64pe? I think so, but I wait for developers response.

If you want our order of operations, check inside the code for my math evaluation routine.  Math Evaluator (qb64phoenix.com)

For our logical operators, they're last in the list and in this order:

Code: (Select All)
 'Logical Operations PL 80+
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "NOT"
    ReDim _Preserve PL(i): PL(i) = 80
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "AND"
    ReDim _Preserve PL(i): PL(i) = 90
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "OR"
    ReDim _Preserve PL(i): PL(i) = 100
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "XOR"
    ReDim _Preserve PL(i): PL(i) = 110
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "EQV"
    ReDim _Preserve PL(i): PL(i) = 120
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "IMP"
    ReDim _Preserve PL(i): PL(i) = 130

So all AND statements will be resolved before OR statements, much like how multiplication is resolved before addition or subtraction.  (PL is Priority Level.  Lower goes first over higher.)

One note though:  NOT is an awfully strange beast.  It's of the highest priority and yet also of the lowest priority.  Unique handling is required when parsing it, as it doesn't truly fit into our order of operations like you'd expect.  It's rather confusing to understand fully, and not worth derailing this topic here with it, but if you're truly interested in how it's parsed, start another topic on NOT somewhere, and I'll refresh my memory and try to explain the PITA that it is for you.  If you're just mildly interested, and don't want a headache from it, just keep in mind, "NOT is a special beast when parsing and order of operations.  Ask more about it if it ever comes up that I need to implement proper evaluation order myself with it."  Wink
Reply
#12
NOT a problem.

I'm in the camp that Tempo mentioned, use of home-made logical operators. This bitwise and XOR, NOT, etc. are fun, but can be more complicated than most of the conditions I code for.

For routines like this mouse or any simple condition that even only requires two conditions met, just everyone please remember to only use a numeric representation just once in the conditional statement...

Notice these differences if we want a non-zero response between i and j...

Code: (Select All)
i = 1: j = 1
PRINT i AND j ' bitwise returns 1
PRINT i AND j > 0 ' bitwise returns 1
PRINT i > 0 AND j > 0 ' numeric returns -1
PRINT "--------------------"
PRINT i AND j < 0 ' zero
PRINT i > 0 AND j > 0 ' negative one
PRINT "--------------------"
x$ = "abc"
find$ = "b"
j = INSTR(x$, find$)
IF j AND LEN(find$) THEN PRINT "1" ELSE PRINT "other"
IF j AND LEN(find$) <> 0 THEN PRINT "2" ELSE PRINT "other"
IF j <> 0 AND LEN(find$) THEN PRINT "3" ELSE PRINT "other"
IF j <> 0 AND LEN(find$) <> 0 THEN PRINT "4" ELSE PRINT "other"
PRINT "--------------------"
So especially in that last example the coder who thinks IF j AND LEN(find$) is the same as IF j <> 0 AND LEN(find$) <> 0 is going to get a sad awakening. If fact, 2nd and 3rd ways of writing the condition are still using a bitwise element, but are solid in there logic for this condition. I like using the second or third method, as it saves me some typing.

Pete
Shoot first and shoot people who ask questions, later.
Reply
#13
(12-11-2022, 02:51 PM)SMcNeill Wrote: https://qb64phoenix.com/forum/showthread.php?tid=1187  <-- Read this for EQV, and then see if you have any questions.  Wink

(12-11-2022, 08:21 PM)SMcNeill Wrote:
(12-11-2022, 05:39 PM)TempodiBasic Wrote: wow I  googled and found the order of execution of the logical operator in QB
from first to last: NOT AND OR XOR EQV IMP


is this the same order of execution made by QB64pe? I think so, but I wait for developers response.

If you want our order of operations, check inside the code for my math evaluation routine.  Math Evaluator (qb64phoenix.com)

For our logical operators, they're last in the list and in this order:

Code: (Select All)
 'Logical Operations PL 80+
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "NOT"
    ReDim _Preserve PL(i): PL(i) = 80
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "AND"
    ReDim _Preserve PL(i): PL(i) = 90
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "OR"
    ReDim _Preserve PL(i): PL(i) = 100
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "XOR"
    ReDim _Preserve PL(i): PL(i) = 110
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "EQV"
    ReDim _Preserve PL(i): PL(i) = 120
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "IMP"
    ReDim _Preserve PL(i): PL(i) = 130

So all AND statements will be resolved before OR statements, much like how multiplication is resolved before addition or subtraction.  (PL is Priority Level.  Lower goes first over higher.)

One note though:  NOT is an awfully strange beast.  It's of the highest priority and yet also of the lowest priority.  Unique handling is required when parsing it, as it doesn't truly fit into our order of operations like you'd expect.  It's rather confusing to understand fully, and not worth derailing this topic here with it, but if you're truly interested in how it's parsed, start another topic on NOT somewhere, and I'll refresh my memory and try to explain the PITA that it is for you.  If you're just mildly interested, and don't want a headache from it, just keep in mind, "NOT is a special beast when parsing and order of operations.  Ask more about it if it ever comes up that I need to implement proper evaluation order myself with it."  Wink

Exactly what I was hoping to see. Now I know why, AND before OR before EQV. You guys went on a tear after I went to sleep (GMT +8). Thanks again everyone  Big Grin

Edit: A quick thought, might want to make a note that the table in the wiki is in the order of Boolean operations as well as what was discussed here, would helped me without showing off the EQV in a program. Hell, might make a good wiki page altogether, order of operations. Love learning something new every day!
Reply
#14
@Nasacow
hi, play with this general purpouse function made with EQV and the difference with that made with AND...
Code: (Select All)
Print "EQV"
Print IsInTheRangeEQV(3, 9, 7) ' 7 is in the range 3-9
Print IsInTheRangeEQV(9, 3, 7) ' 7 is in the range 3-9
Print IsInTheRangeEQV(3, 7, 9) ' 9 is above range 3-7
Print IsInTheRangeEQV(9, 7, 3) ' 3 is under range 7-9

Print "------------------"
Print "AND"
Print IsInTheRangeAND(3, 9, 7) ' 7 is in the range 3-9
Print IsInTheRangeAND(9, 3, 7) ' 7 is in the range 3-9
Print IsInTheRangeAND(3, 7, 9) ' 9 is above range 3-7
Print IsInTheRangeAND(9, 7, 3) ' 3 is under range 7-9
Print "if you look closely at code you can see what advantage EQV brings up versus AND for this kind of test"
Print "--------OutOfRangeEQV"
Print OutOfRangeEQV(3, 7, 1)
Print OutOfRangeEQV(7, 3, 1)
Print OutOfRangeEQV(3, 9, 7)
Print OutOfRangeEQV(9, 3, 6)
Print OutOfRangeEQV(3, 7, 9)
Print OutOfRangeEQV(7, 3, 9)
Sleep 2
Cls , 2
Locate 1, 1
Print "now we go to test in action"
Sleep 2
Cls , 3
For a = 1 To 10
    Locate 10 + a, 20: Print Space$(20)
Next a

While _MouseButton(2) = 0
    If _MouseInput Then
        Locate 1, 20: Print Space$(30);
        If IsInTheRangeEQV(10, 21, _MouseY) Then
            If IsInTheRangeEQV(19, 41, _MouseX) Then
                Locate 1, 20: Print Space$(30);
                Locate 1, 20: Print "the mouse is in the box!"
            End If
        End If
    End If
    'clear the buffer of mouseinput
    While _MouseInput: Wend
Wend
Cls , 0
Print " end of demo"
End

Function IsInTheRangeAND (Min As Integer, Max As Integer, Value As Integer)
    ' this function returns true if Value is less than Max and more than Min
    If (Min < Value) And (Value < Max) Then IsInTheRangeAND = -1 Else IsInTheRangeAND = 0
End Function


Function IsInTheRangeEQV (Min As Integer, Max As Integer, Value As Integer)
    ' this function returns true if Value is less than Max and more than Min
    If (Min < Value) Eqv (Value < Max) Then IsInTheRangeEQV = -1 Else IsInTheRangeEQV = 0
End Function

Function OutOfRangeEQV (Min As Integer, Max As Integer, Value As Integer)
    ' this function returns true if Value is more than Max and Min
    If (Min < Value) Eqv (Max < Value) Then IsMaxEQV = -1 Else IsMaxEQV = 0
    ' If (Min > Value) Eqv (Min > Max) Then IsMinEQV = -1 Else IsMinEQV = 0 REM this brings to the same results
End Function

About Logical test versus Bitwise test... the second is for who works with &H or &B digital numbers... ASM/C at low level and any lowlevel language... the logical test is for instruction of choice in the code... you can use only the direct comparison operators (= <> > < >= <= ) or also logical operators...

IMHO in Qbasic and in QB64 AND/OR/XOR/NOT/EQV/IMP  work as bitwise mode when you pass them numbers... and as logical mode when you pass comparisons.
Fine to learn from you boys of QB64! Heart
Reply




Users browsing this thread: 2 Guest(s)