Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

executor_2k2

How do I check if 2 numbers are oppositely signed in C++

This topic is 6098 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

How do I check if two numbers have the same sign or not. I may be missing something pretty obvious, but please help. Oh btw. I would like the fastest way plz. Thx

Share this post


Link to post
Share on other sites
Advertisement
  
int a = -7;
int b = 3;

if((a < 0 && b >= 0) || (a >= 0 && b < 0))
cout << "The numbers have opposite signs";


Hopefully this wasn''t a homework assignment...

Cheers

Share this post


Link to post
Share on other sites
The mechanism you use depends on how tied to the hardware you want to be, and what kinds of numbers you're talking about.


Edited by - DrPizza on February 3, 2002 9:41:56 PM

Share this post


Link to post
Share on other sites
By sign do you mean +/- ? If so you simply need to do two tests to see if each number is > or < (or = to) 0. Then compare the two results. Here''s an example in C++:


// returns true if a and b have same sign, otherwise returns false
void sameSign(int a, int b){
if (a < 0 && b < 0)
return true;
else if (a > 0 && b > 0)
return true;
else if (a == 0 && b == 0)
return true;
else
return false;
}

"I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites
You''re right. It''s pretty obvious.

How do we know that 5 and 8 have the same sign? Because they''re both greater than 0.

How do we know that -3 and -6 have the same sign? Because they''re both less than 0.

DOH. Just looked down and saw that a billion people have already replied. No use in driving it in any more

Share this post


Link to post
Share on other sites
For 32bit processors (and 32bit signed numbers) only:
  
if( (a & 0x80000000) ^ (b & 0x80000000) ) {
/*
The numbers have opposite signs.
*/
} else {
/*
The numbers have the same sign.
*/

}


Share this post


Link to post
Share on other sites
The only thing is his method doesn''t handle zero''s very well. While 3 and 0 have different signs (3*0 = 0), two zeros technically have the same sign, but 0*0 = 0 would suggest they are different. It all depends on how you want to handle such a function.... why you would need one that does this is beyond me though.

Hehe, I too was surprised by the quick responses to this post =-)

"I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites

  • 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!