simpler way?

Blitz3D Forums/Blitz3D Beginners Area/simpler way?

Second Chance(Posted 2003) [#1]
Is there a shorter, simpler way to write this?

IF (x=0) OR (y=0) OR (z=0) = TRUE THEN blah,blah,blah

Thanks


Peer(Posted 2003) [#2]
If x*y*z = 0 Then Bla, Bla, Bla...


Anthony Flack(Posted 2003) [#3]
Nope, that would be

IF (x=0) AND (y=0) AND (z=0) = TRUE THEN blah,blah,blah


Skysaw(Posted 2003) [#4]
Actually, Peer was right. x*y*z = 0 if any of x, y or z are 0, so that's an OR.


Yappy(Posted 2003) [#5]
if x+y+z<3


Skysaw(Posted 2003) [#6]
That doesn't work. That would return true more than 50% of the time using random integers. For example x=10, y=14, z=-38


Anthony Flack(Posted 2003) [#7]

Actually, Peer was right. x*y*z = 0 if any of x, y or z are 0, so that's an OR.



Oh, I had an exceptionally stupid moment...!

Um.. to do the and, I guess you'd use ORs?

if x OR y OR z =0

=

if (x=0) and (y=0) and (z=0)


Second Chance(Posted 2003) [#8]
Thanks guys, it looks like Anthony's last one is the one I want. I guess I didn't think to say that there's a 50% chance one, or all three, of the integers could be negative. So it has to work with frequent, multiple negative numbers.

And it's so hard to work with numbers that are frequently negative, they always try to bring you down :)


Anthony Flack(Posted 2003) [#9]
PLease note - my last eg was for AND (even though I used ORs to achieve the ANDy effect) - um, I think...

Okay, I checked, and yup, it works, providing you use brackets

IF (X OR Y OR Z)=0 THEN...
is the same as
IF (X=0) AND (Y=0) AND (z=0)

otherwise, without the brackets, it's read as

IF (x=1) OR (y=1) OR (z=0) THEN...


Note though - like the other eg, this is only good for checking against zero...


Second Chance(Posted 2003) [#10]
I'm confused! What I need to know is if ANY of the numbers equals zero. Could be one, could be two, or it could be all three. As long as any one of them equals zero I want the other code to be executed. Does this do that? Your explanation looks like it's contrary to your code. I'm getting really confused. 8)

Thanks for trying everyone :)


Floyd(Posted 2003) [#11]
Your original method didn't need the "= True", so...

IF (x=0) OR (y=0) OR (z=0) Then do something.

Edit: I was going to say the first reply was also correct:

If x*y*z = 0 Then ...

Both of them test whether at least one of x,y,z is zero.

But in the computer world of only finitely many integers this can fail. Consider:

x = 1024
y = 2048
z = 2048

Print x * y * z ; oops

So stick with the first method.


Second Chance(Posted 2003) [#12]
Thanks Floyd, at least it trims a tiny bit off. That's still better than nothing.


Warren(Posted 2003) [#13]
Seems like a lot of effort to eliminate very little code bloat.

Just my opinion.


jhocking(Posted 2003) [#14]
Seriously. This smacks of something I've been told and have experienced; your optimizing efforts are better spent figuring out better ways of accomplishing tasks (eg. a faster shadow casting algorithm) than nitpicking text. Plus, for a game if you find yourself trying to optimize your "if" statements you ought to be looking to reduce your polygon count are color depth or something; the graphics load has a MUCH bigger influence on performance than the wordiness of your code.


Blue Steel(Posted 2003) [#15]
bigger influence on performance than the wordiness of your code.
Yup we have no way of knowing how the variations are handled at compile time. All examples shown could result in the exact same code when compiled...


Anthony Flack(Posted 2003) [#16]
I was assuming this was all in aid of speeding up typing...


Skysaw(Posted 2003) [#17]
I was assuming it was just a simple exercise provided as diversion.


Sir Gak(Posted 2003) [#18]
Help, my brain is bleeding...


Second Chance(Posted 2003) [#19]
Actually, it was just an excercise in keeping the code manageable and neat. Purely for my own needs to read it later. Thanks for all the good points that were made. "The mastery comes not from memorizing the information, but from understanding the application of it"...me :)
Since I'm still a fledgling programmer everything I understand now will shape how I learn the next thing. So thanks :)


Skysaw(Posted 2003) [#20]
Manageable and neat are subjective, but I can't imagine anyone finding any of the alternatives more readable than: IF (x=0) OR (y=0) OR (z=0)