a = t > 5 ???

BlitzMax Forums/BlitzMax Beginners Area/a = t > 5 ???

Chroma(Posted 2010) [#1]
I was rustling around in the BMax source graphics.bmx and came across this line. I stared at it for awhile and went...uhhh...no way...

After a quick test...

'a = t > 5' is the same as 'If t > 5 Then a = t'

Very cool.


Warpy(Posted 2010) [#2]
No, it's the same as 'If t>5 then a=True Else a=False'

't > 5' is a boolean expression, which evaluates to either True or False. a is assigned that value.


Chroma(Posted 2010) [#3]
Interesting.

Last edited 2010


Czar Flavius(Posted 2010) [#4]
Perhaps you want to check the same condition multiple times? And want to save some cycles from recalculating.

Local condition:Int = (x >= min And x < max)
If condition Then ..........
..........
If condition Then ..........

Brackets often aren't needed, but they are a good idea for clarity.

In Blitz, True is defined as 1 and False is defined as 0, so you can do some tricks. Do you want to add a only if x or y is true?

arbitrary conditon
x = (w And v) Or z
y = hedgehog > 0

b = d + e + f + a * (x Or y)

Personally, I avoid shortcuts such as those as they can be confusing, but it's good to know they are out there.

Last edited 2010


Chroma(Posted 2010) [#5]
This one is tripping me up.

x = (w And v) Or z


Czar Flavius(Posted 2010) [#6]
it means w and v are And-ed together, and the result is then Or-ed with z.

Example, w is 1, v is 0 and z is 1.

(w And v) Or z
(1 And 0) Or 1
0 Or 1
1


You might have

(InRange And HasAmmo) Or SuperPowerup

The condition is true, if you are in range and have ammo, OR if you have the super power up, regardless of range and ammo.

Any number which is not 0 will be converted to true for the purposes of a boolean operation. So w=5, v=0 and z=-1 will give the same result as above. Yes, negative numbers are considered true!

References can also be used in boolean operations. A Null reference is false, and a valid reference is true.


Local o:MyObject = GetObject()
If o Then o.DoSomething()

So you are checking that o is not null before doing something with it.

If you are getting o1:MyObject and o2:MyObject as function parameters, and both must be valid, you can put this at the top of the function.

Assert o1 And o2.


Chroma(Posted 2010) [#7]
Ah, and each var is basically seen as either 1 or 0.

As in If (w<>0 and v<>0) Or z<>0 Return 1 Else Return 0

I understand the Null object checking and do it where needed.

It was just the w & v stuff. Or is the & sign even different?

I've always like seeing checks with very small footprints but some of it was a bit deterring on sight. But this is clear. Thanks.


Chroma(Posted 2010) [#8]
And as you can tell from looking at my code; I'm a minimalist. I hate bloated unnecessary code. I'll write something on first pass just to get it working. Then go back and refine and trim it down to as little code as possible.

For instance:

Function isPositive(val)
   If val>0 Then Return 1 Else Return False
End Function


Even something as small as above, I'll rewrite to this eventually:
Function isPos(val)
   Return val>0
End Function

I'm pretty sure I have some coding OCD or something. :)

Last edited 2010


Htbaa(Posted 2010) [#9]
The latter is how you should write it as it's more clear and you're returning a boolean value anyway.

Last edited 2010


Czar Flavius(Posted 2010) [#10]
I'd return the second value, but keep the first name. I hate code that has so many abbreviations it feels like you need a dictionary just to understand it. isPos is ok, but contractions such as msk instead of mask I don't like. It makes it harder to read and doesn't save you any real typing.


Chroma(Posted 2010) [#11]
This is pretty cool:

Graphics 800,600
While Not KeyHit(KEY_ESCAPE)
Cls
   a = Loop(a,99)
   DrawText a,10,10
Flip
Wend
End

Function Loop(num,loop)
   num :+ (num<loop) or - loop
   Return num
End Function


It counts up from 0 to loop then resets to 0 and starts over.


AdamRedwoods(Posted 2010) [#12]
Also know the difference between LOGICAL AND-ing and OR-ing versus BIT-WISE "&" and bitwise "|":



RESULTS:
a = 15
b = 17
a and b:17
a & b:1
a or b:15
a | b:31


Perhaps the logical a AND b is equivalent to MAX(a,b) and OR is MIN(a,b)??
EDIT: not in the slightest. did a for next loop through the numbers.

a =0
b=$11
Print "a or b:"+(a Or b) ''returns b
a=$01
Print "a or b:"+(a Or b) ''returns a, if a<>0


Last edited 2010

Last edited 2010


Czar Flavius(Posted 2010) [#13]
Tricks like that are best avoided as it's difficult to see what's going on. It's not immediately clear the value is -loop if the first condition is false.


Chroma(Posted 2010) [#14]
If you wrote it then you know what's going on. Probably true with about 100% of everyone's own special purpose functions.


Czar Flavius(Posted 2010) [#15]
You say that now, but after you have 100 special purpose functions and you haven't used them for 5 months.. I'm not saying don't use them when they are appropriate, but for simple things like that regular For loops/If statements would not only be more readable, but probably faster..


seyhajin(Posted 2011) [#16]
Try it : a = ( t > 5 ) * t

if true a = t else if false a = 0