Jump to content
  • Advertisement
Sign in to follow this  
darenking

Testing lots of variables with == == ==

This topic is 4874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just a simple thing. I want to test if a bunch of variables hold the value zero. I could do it like this: if ( a + b + c ) == 0 ) But I have a hunch there's a more elegant way of doing it, something like: if ( a == b == c == 0 ) Doesn't seem to work though. Maybe it attempts to change the value of a?

Share this post


Link to post
Share on other sites
Advertisement
What you do with the first test is to check if the sum is 0, so a=1, b=2, c=-3 would evaluate as true. In the second test, you first check if a is the same as b, then if the result of that is the same as c and finally if that result is 0, which isn't what you want. The test I think you want to do is a==0 && b==0 && c==0.

Share this post


Link to post
Share on other sites
check the associativity problem
let's say == is right associative
then you got:
( a == b == c == 0 )
<=>
( a == ( b == ( c == 0 ) ))
for a=1,b=1,c=0 we get 1==(1==(0==0)) <=> 1==(1==1) <=> 1==1 <=> 1

same for left associativity(only you have to set a=0,b=1,c=1

P.S.And == is associative
P.S.P.S. Check a==0 && b==0 && c==0 or,better !(a||b||c)

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
What about:


if((a | b | c) == 0)


na, using my solution gives the compiler the option to early out, if you use bitwise instead of logical or all arguments would need to be evaluated.

Share this post


Link to post
Share on other sites
Quote:
Original post by vallentin
@DigitalDelusion
so what.It goes out on a!=0 or b!=0 or c!=0 which is abs.normal


let's generalize a bit and make a,b and c into functions returning int's instead and lets see what happens:

a() | b() | c()
will always call all three functions
a() || b() || c()
will only evaluate until any function returns a non zero value

if you mean logical or use logical or, this falls squarely under the KISS principle.

[edit]: also note that one of them has a clearly defined evaluation order.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!