Page 1 of 2
Handling boolean expressions
Posted: Tue Jan 16, 2007 3:48 pm
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))
Posted: Tue Jan 16, 2007 3:51 pm
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)))
Posted: Tue Jan 16, 2007 3:56 pm
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.
Posted: Tue Jan 16, 2007 5:08 pm
by AND51
Hm... OK! But in my special case I need quads. Nevertheless, this bug must be fixed.
Posted: Tue Jan 16, 2007 5:47 pm
by freak
This kind of expression is not supported in PureBasic.
You can only expect them to work right in If, While, etc statements.
Posted: Tue Jan 16, 2007 6:38 pm
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
Posted: Tue Jan 16, 2007 6:45 pm
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.
Posted: Tue Jan 16, 2007 7:24 pm
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?
Posted: Tue Jan 16, 2007 7:27 pm
by freak
Last time we talked about it, Fred said he does not plan it for now.
Posted: Tue Jan 16, 2007 7:30 pm
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...
Posted: Tue Jan 16, 2007 8:00 pm
by AND51
thx, kaeru gaman
@ freak: So the help was an empty promise?
No... No Boolean expressions?

Sniff...
Posted: Tue Jan 16, 2007 8:04 pm
by Kaeru Gaman
no, it was only put into the help by accident.
the help is for functionallities, not for Tips&Tricks....
Posted: Tue Jan 16, 2007 8:57 pm
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.
Posted: Tue Jan 16, 2007 9:08 pm
by AND51
Unfortunately, I don't really understand...? Can you explain it in other words again and/or give me an example code?
Posted: Tue Jan 16, 2007 9:16 pm
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