Logical AND in PB vs && in C++

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Logical AND in PB vs && in C++

Post by SFSxOI »

In the C language the logical AND operator (&&) returns the boolean value true if both operands are true and returns false otherwise. In PB the AND operator does the same thing.

In C++ I would do something like this maybe

Code: Select all

BOOL enabled = FALSE;
result = SUCCEEDED(::SomeFunction(&enabled)) && enabled;
In PB I find that the results of a comparable expressions like this:

Code: Select all

enabled.b = #False
result = CallFunction(Lib, "SomeFunction) AND enabled
don't return the same result. So why would this happen? Isn't the PB function version 'result = CallFunction(Lib, "SomeFunction) AND enabled' the same as the C++ version in doing this?
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

But you are ANDing the result with false so the answer will always be false.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Boolean Expressions are NOT implemented in PB.
it's not possible to calculate boolean values.

it's an already requested feature, but it's not clearly said if it will ever be implemented.
oh... and have a nice day.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Kaeru Gaman wrote:Boolean Expressions are NOT implemented in PB.
it's not possible to calculate boolean values.

it's an already requested feature, but it's not clearly said if it will ever be implemented.
@Derek - your right I think, but looking below your post...

@Kaeru - Ahhhhh...Ok then, there is the answer. But it is possible to evaluate boolean expressions. So then your saying that replacing the C++ '&&' with the PB 'AND' is not the same then?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

SFSxOI wrote:But it is possible to evaluate boolean expressions.
NOT in PB!

there is a trick, you can evaluate it if you make a kind of Bool-Cast to the expression:

Code: Select all

((A > B) Or #False)
http://www.purebasic.fr/english/viewtop ... 870#177870

but it is ONLY a trick, it does ONLY work with Long, and there is NO guarantee that it will be supported on ANY future PB-Version.

so, better think on a different solution, "If" in PB is really, really fast.
Last edited by Kaeru Gaman on Mon Jan 29, 2007 2:06 pm, edited 1 time in total.
oh... and have a nice day.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Logical AND in PB vs && in C++

Post by Derek »

SFSxOI wrote:

Code: Select all

BOOL enabled = FALSE;
result = SUCCEEDED(::SomeFunction(&enabled)) && enabled;
But I would of thought that enabled should be TRUE and the same for the PB version.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

*sigh*

but

Code: Select all

result = (A-condition) And (B-condition)
is a Boolean calculation and they are just not implemented in PB.
so maybe sometimes you'll get the right result, but sometimes not and there is no guarantee for nothin'
oh... and have a nice day.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Kaeru Gaman wrote:*sigh*

but

Code: Select all

result = (A-condition) And (B-condition)
is a Boolean calculation and they are just not implemented in PB.
so maybe sometimes you'll get the right result, but sometimes not and there is no guarantee for nothin'

I don't see anything wrong with:

Code: Select all

a =1
b = 2

If a = 1 And b = 2
  Debug "Yippee!"
EndIf
I may look like a mule, but I'm not a complete ass.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Kaeru Gaman wrote:*sigh*

but

Code: Select all

result = (A-condition) And (B-condition)
is a Boolean calculation and they are just not implemented in PB.
so maybe sometimes you'll get the right result, but sometimes not and there is no guarantee for nothin'
I'm not arguing that they are not implemented, just the fact that if they were and you ANDed a boolean result with false then you would always get false as the answer which is of no use to anyone, you should AND with true and then you can get a true or false answer.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Would not:

Code: Select all

a =1 
b = 2 

If a = 1 And b = 2 
  Debug "Yippee!" 
EndIf
be the same as:

Code: Select all

enabled = #False ; a 0 
result = CallFunction(Lib, "SomeFunction) ; could be >0 or a 1 or a 0
If enabled = #False And result = 0 ; or even result <> 0, or maybe result >0
Debug "Yipee"
EndIf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

srod wrote:I don't see anything wrong with:

Code: Select all

a =1
b = 2

If a = 1 And b = 2
  Debug "Yippee!"
EndIf
because there is nothing wrong with it they way you notated it.

what they meant was:

Code: Select all

a =1
b = 2

result = (a = 1) And (b = 2)

If result
  Debug "Yippee!"
EndIf
-----------------------------------------------
SFSxOI wrote:Would not:

Code: Select all

be the same as: 
[CODE B][/quote]
exactly, and that is the way you can do it in PB.
but you cannot calculate a result out of conditions, because
[b]Boolean Expressions and Boolean Calculations are not supported in PureBASIC[/b]


it's not a question of what i prefer, i would like to use them,  but it is simply not implemented.

PS:
[quote="Derek"]... if they were ...[/quote]
if they were implemented you were right. I wish you were. :wink:
oh... and have a nice day.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Kaeru Gaman wrote:
srod wrote:I don't see anything wrong with:

Code: Select all

a =1
b = 2

If a = 1 And b = 2
  Debug "Yippee!"
EndIf
because there is nothing wrong with it they way you notated it.

what they meant was:

Code: Select all

a =1
b = 2

result = (a = 1) And (b = 2)

If result
  Debug "Yippee!"
EndIf
which still works. :D
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

@Kaeru

Ahhhh...OK then. I guess I was confusing 'evaluating' the expression with 'calculating the result' of an expression. They are not the same...well at least we can evaluate in some fashion, its something, but it would be really nice if PB had Boolean Expressions and Boolean Calculations.

I wonder why they haven't been implemented in PB yet? Would it be to difficult to do so in terms of a major shift in the fundamental way PB is implemented now? I wonder if it would be too difficult to build something like an add on library that would do it?
Last edited by SFSxOI on Mon Jan 29, 2007 3:51 pm, edited 1 time in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Derek wrote:which still works. :D
...what is an absolutely meaningless coincidence. ;)

and now I'll go and have a cup of tea with Arthur Dent and Dirk Gently...

--------------------------------------------------------------------------
SFSxOI wrote:I wonder why they haven't been implemented in PB yet? Would it be to difficult to do so in terms of a major shift in the fundamental way PB is implemented now? I wonder if it would be too difficult to build something like an add on library that would do it?
http://www.purebasic.fr/english/viewtop ... 815#177815
oh... and have a nice day.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Well then, based upon this:
http://freak.purearea.net/v4/ReadMe.html#xor

could something like this be done?:

Code: Select all

enabled = #False ; a 0
If CallFunction(Lib, "SomeFunction) Not enabled
   Debug "OK"
EndIf
Or am I still looking at this from a calculation rather then an evaluation view point?
Post Reply