Page 1 of 1

And/Or operations

Posted: Wed Jun 08, 2016 8:58 pm
by ANDY ANDERSON

Code: Select all

If SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R" Or REF$ = "1": Goto SKIPSOMETHING: EndIf
; Do Something
SKIPSOMETHING:
In PowerBasic (which I'm converting), the above code is equivalent to:

Code: Select all

If SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R": Goto SKIPSOMETHING: EndIf
If REF$ = "1": Goto SKIPSOMETHING: EndIf
; Do Something
SKIPSOMETHING:
But apparently in PureBasic it is not. If REF$ equals 1, it still falls through
to the Do Something code. But if I do the below, it works as intended. Note
the parenthesis.

Code: Select all

If (SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R") Or REF$ = "1": Goto SKIPSOMETHING: EndIf
; Do Something
SKPSOMETHING:
Being relatively new to PureBasic, I'm assuming there is a difference in the way
PureBasic handles And/Or operations.

Would someone share their thoughts, please?

Thanks in advance.

Re: And/Or operations

Posted: Wed Jun 08, 2016 9:18 pm
by DontTalkToMe
You should at least mention what compiler you are using.
ANDY ANDERSON wrote: Being relatively new to PureBasic, I'm assuming there is a difference in the way
PureBasic handles And/Or operations.
Yes, sometimes boolean expressions are bugged, but in this case

Code: Select all

REF$ ="1"

If SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R" Or REF$ = "1": Goto SKIPSOMETHING1: EndIf
Debug "Do Something 1"
SKIPSOMETHING1:
Debug "skipped 1"

If SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R": Goto SKIPSOMETHING2: EndIf
If REF$ = "1": Goto SKIPSOMETHING2: EndIf
Debug "Do Something 2"
SKIPSOMETHING2:
Debug "skipped 2"

If (SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R") Or REF$ = "1": Goto SKIPSOMETHING3: EndIf
Debug "Do Something 3"
SKIPSOMETHING3:
Debug "skipped 3"

Code: Select all

skipped 1
skipped 2
skipped 3
Are you sure ?

Re: And/Or operations

Posted: Wed Jun 08, 2016 9:41 pm
by ANDY ANDERSON
Thanks, Dont Talk to Me.

I'm using 5.42 LTS x64.

Yes, I'm sure. It took me the better part of a day to trap it.

Maybe someone with my compiler version can try it.

If it is a bug, it's the very first one I've found in PureBasic.

Re: And/Or operations

Posted: Wed Jun 08, 2016 9:59 pm
by DontTalkToMe
ANDY ANDERSON wrote: I'm using 5.42 LTS x64.
...
Maybe someone with my compiler version can try it.
I tried with 5.42 x64 on windows, do you get a different result ?

Code: Select all

REF$ ="1"

If SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R" Or REF$ = "1": Goto SKIPSOMETHING1: EndIf
Debug "Do Something 1"
SKIPSOMETHING1:
Debug "skipped 1"

If SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R": Goto SKIPSOMETHING2: EndIf
If REF$ = "1": Goto SKIPSOMETHING2: EndIf
Debug "Do Something 2"
SKIPSOMETHING2:
Debug "skipped 2"

If (SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R") Or REF$ = "1": Goto SKIPSOMETHING3: EndIf
Debug "Do Something 3"
SKIPSOMETHING3:
Debug "skipped 3"

Debug #PB_Compiler_Version
Debug Bool(#PB_Compiler_Processor = #PB_Processor_x64)                              

Code: Select all

skipped 1
skipped 2
skipped 3
542
1
ANDY ANDERSON wrote: If REF$ equals 1, it still falls through to the Do Something code.
It jumps over it, so I don't understand. Am I misunderstanding you ?

Re: And/Or operations

Posted: Wed Jun 08, 2016 11:18 pm
by ANDY ANDERSON
Try this. The SRC$ value will cause the first argument to be FALSE. The REF$ value
will cause the second argument to be TRUE. In Boolean "OR" logic, the result
should be TRUE.

Code: Select all

SRC$ = "F": REF$ = "1"

If SRC$ <> "F" And SRC$ <> "K" And SRC$ <> "R" Or REF$ = "1": Goto SKIPSOMETHING: EndIf
MessageRequester("TEST", "Not supposed to happen")
SKIPSOMETHING:
MessageRequester("TEST", "Completed")
End

Re: And/Or operations

Posted: Wed Jun 08, 2016 11:43 pm
by DontTalkToMe
Andy... Andy... that's a lot different from what you said, now you added SRC$ = "F" to the REF$ = "1" you mentioned.

:cry: :cry: :cry:

Then yes, looks like one of the possible variants of the recent bug discovered in 2009 I linked in my first post.

Trond wrote: 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
Words of gold encrusted with diamonds.

Using () for clarity may be ok if you really want to, but being forced to do so to work around a bug it's not.

Re: And/Or operations

Posted: Thu Jun 09, 2016 12:24 am
by ANDY ANDERSON
@DontTalkToMe

Thanks again for taking the time to test things.

I agree with Trond's thoughts but in my case, I'm desparately trying to convert 300 programs
with which I make my living to PureBasic. Therefore, I must be a pragmatist. But, do you think
I should post the code in the Bug reporting forum ?

As an afterthought, why doesn't Fred fix this ?

As far as the conversion, I'm halfway there.

Re: And/Or operations

Posted: Thu Jun 09, 2016 12:30 am
by DontTalkToMe
ANDY ANDERSON wrote: I agree with Trond's thoughts but in my case, I'm desparately trying to convert 300 programs
with which I make my living to PureBasic. Therefore, I must be a pragmatist.
I understand.
ANDY ANDERSON wrote: But, do you think I should post the code in the Bug reporting forum ?
According to what Fred always said no, since it has been already reported. You have my sympathy.

On the other hand you are a paying customer, the bug was reported in 2009, maybe you could reply in the original thread and link this one there to show another example.
That way you are not creating a new report and what you have indipendently discovered is added to the list.
Don't know how much good it would do.
ANDY ANDERSON wrote: As an afterthought, why doesn't Fred fix this ?
You should ask him, I could only tell you my theory about it.

Good luck with your conversion. :wink:

Re: And/Or operations

Posted: Thu Jun 09, 2016 7:42 am
by HanPBF

Code: Select all

if a and b and c or d
endif
if a or b and c
endif
This is always wrong!
What is meant there???
Is it short circuit?

Never ever use or / and without brackets!

I'd even use brackets here:

Code: Select all

1 + (2 * 3)

Re: And/Or operations

Posted: Thu Jun 09, 2016 11:55 am
by Dude
Deleted (misread the example, so my answer was totally wrong). :oops:

Re: And/Or operations

Posted: Fri Jun 10, 2016 12:31 pm
by DontTalkToMe
HanPBF wrote: This is always wrong!
No, you just don't like it because it confuses you. That's ok (maybe) but it doesn't make it wrong. IT IS NOT WRONG.
Dude wrote: Sure; the explanation is here: http://www.purebasic.fr/english/viewtop ... 46#p489446 ;)
That was... interesting. :shock:

Re: And/Or operations

Posted: Fri Jun 10, 2016 1:31 pm
by Dude
DontTalkToMe wrote:That was... interesting. :shock:
I know. :oops: See above.