Handling boolean expressions

Just starting out? Need help? Post your questions and find answers here.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Handling boolean expressions

Post by AND51 »

Hello!

This code is very, very similar to the signurm function. To get the signum function, just replace ">=" by ">".

My problem: This code just should return 1, if the number is =0 or >0. it should return -1 if number <0.

The first line is correct, but i can be shorter. Look at the second line. Its does exactly the same, but the returned valu is wrong!

Code: Select all

Define number.q=-5

Debug (-(number < 0 And 1)+(number >= 0 And 1))
Debug ((number >= 0 And 1)-(number < 0 And 1))
PB 4.30

Code: Select all

onErrorGoto(?Fred)
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Another thing I've just discovered:
I don't know, if this is a bug, but this message is not useful for me. Perhaps you can do something with it:

Code: Select all

Define number.q=-5
Debug StrQ(number*(-(number < 0 And 1)+(number >= 0 And 1)))
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

It's just another bug to do with Quads. Seems to be a lot of them.

Changing to .w .b .l etc and you get the right answer.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Hm... OK! But in my special case I need quads. Nevertheless, this bug must be fixed.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

This kind of expression is not supported in PureBasic.
You can only expect them to work right in If, While, etc statements.
quidquid Latine dictum sit altum videtur
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

The manual says something else!!
Sorry, I only have the german version of this help.
Variablen, Typen, Operatoren wrote:Spezielle Informationen zur Verwendbarkeit logischer Ausdrücke

Logische Ausdrücke (z.B. "Position < 100") können uneingeschränkt mit Befehlen verwendet werden, die logische Ausdrücke erwarten (z.B. If, While, Until) sowie in Kombination mit logischen Operatoren (And, Or, XOr, Not).

In der vorliegenden PureBasic-Version liefern die Rückgabewerte der Vergleichsoperatoren (<, <=, =, >=, >, <>) Ergebnisse, die z.B. in Kombination mit mathematischen Ausdrücken zu unerwarteten Resultaten führen können. Verwenden Sie in solchen Fällen zusätzlich einen der Operatoren And, Or oder Not, um korrekte Ergebnisse zu erzielen.

Beispiel:
Counter = 0 : A = 20 : B = 20
Counter + (A=B) ; Counter wird um den Wert B erhöht (unabhängig vom Vergleichsergebnis)
Counter + (0 Or (A=B)) ; Counter wird um 1 erhöht, wenn der Vergleich #True (wahr) ergibt
Counter + (1 And (A=B)) ; Counter wird um 1 erhöht, wenn der Vergleich #True (wahr) ergibt
Counter + (Not Not (A=B)) ; Counter wird um 1 erhöht, wenn der Vergleich #True (wahr) ergibt
PB 4.30

Code: Select all

onErrorGoto(?Fred)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Well, this was a mistake. Andre added this part without us knowing.
It is removed from the help again for the next version.

PureBasic does not officially support boolean expressions outside the keywords that expect them.
quidquid Latine dictum sit altum videtur
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

@AND

I already told you not to use an expression twice if once is enough.
second, put expressions in brackets.

Code: Select all

Macro XSign(Exp)
  (((Exp)>0 Or 0)*2-1)
EndMacro
as freak said, it's a trick.
neither we can claim functionallity with quads, nor we can be sure of future support.


@freak
any chance, boolean expressions will be added some day?
oh... and have a nice day.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Last time we talked about it, Fred said he does not plan it for now.
quidquid Latine dictum sit altum videtur
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

hm.. i see...

...just keep in mind that more than a handfull of people use this workaround because they are used to boolean expressions from other programming languages.
it would be not too bad an idea to implement it ~some~ day...
oh... and have a nice day.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

thx, kaeru gaman

@ freak: So the help was an empty promise?

No... No Boolean expressions? :cry: :cry: :cry: Sniff...
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

no, it was only put into the help by accident.
the help is for functionallities, not for Tips&Tricks....
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

> Last time we talked about it, Fred said he does not plan it for now.
Hey, he told me it was planned!

AND51: It says (at least in the English help file) that And/Or can be used to make the relational operators (=><) work correctly. It doesn't say that this will make And and Or work correctly.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Unfortunately, I don't really understand...? Can you explain it in other words again and/or give me an example code?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

AND51 wrote:...Can you ... give me an example code?

Code: Select all

If Want_to_Use(Boolean_Expression)
  Helps(Use(And))
EndIf

If Wrongly_Used(And)
  Helps(Nothing)
EndIf
oh... and have a nice day.
Post Reply