• Advertisement

Archived

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

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

This topic is 5862 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
Multiply them and look at the result,
if the result is positive, they have the same sign,
if the result is negative, they have opposite signs.

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
Hey Null and Void ...

Do you know of a way to do that without being processor specific
(i.e. for a 64 bit processor also )

Share this post


Link to post
Share on other sites
In C:
  
#define OppositeSigns(a,b) (( a ^ b ) & (1 << (sizeof(a)<<3)))

In C++:
  
template <class T> inline bool OppositeSigns(const T &a, const T &b) {
return (( a ^ b ) & (1 << (sizeof(a)<<3)));
}

That should work, but it isn''t tested.

Share this post


Link to post
Share on other sites
Thanks

"If patience is a virtue, and ignorance is bliss, you can have a pretty good life if you''re stupid and willing to wait"

Share this post


Link to post
Share on other sites
quote:
Original post by Tac-Tics two zeros technically have the same sign

Not necessarily.

Share this post


Link to post
Share on other sites
quote:
Original post by Martee
[quote]Original post by Tac-Tics two zeros technically have the same sign

Not necessarily.

I know what you mean. sometimes I get -0.0 whenever I print number to a file for some reason, but that just
might be precision problems.


No, HTML is not an OO language.

Share this post


Link to post
Share on other sites
Martee is right, I forgot how it happens, but the way its
setup, zeros can be positive or negative, but not in
mathematics, that's just a product of how computer science
has developed.
The variable would need to be signed in the first place in
order for their to be a sign bit.
Signed zeroes don't affect greater than or less than or
equal to operations I think. It might be an issue if you
only check the sign bit though.
Mathematically, zeroes don't have any sign(again, I think),
so multiplying will always tell if they have different signs
because no sign and having a sign would be different signs.
Is there a mathematician in the house?

Thanks, THE Omega, I'm weird like that, I scored very well
on geometry, algebra, and calculus tests but scored my worst on
the arithmetic portions.

Here's the edit:
I totally forgot about floats. I think it's like masonium
had it, -0.0 happens when a value is obtained that is VERY
close to zero but not quite and is still negative.

Edited by - RolandofGilead on February 3, 2002 11:12:02 PM

Share this post


Link to post
Share on other sites
Indeed. More specifically, if a number is stored internally in ones-complement form, the value 0 may be represented by all 0''s, or by all 1''s. The mathematical concept of 0 doesn''t have a sign, but the representation of a 0 inside a computer may.

Share this post


Link to post
Share on other sites
quote:
Original post by RolandofGilead
Multiply them and look at the result,
if the result is positive, they have the same sign,
if the result is negative, they have opposite signs.

Unless you overflow, in which case the sign bit might not be meaningful.

floats will give you a -0.0 if you approach zero from below, if memory serves.

Share this post


Link to post
Share on other sites
Perhaps the result could be stored in a larger data type.

Perhaps you could divide each beforehand, but that would
slow it down a bit if you had to do it a lot.

Share this post


Link to post
Share on other sites
If you divide, you run the risk of integer underflow, which, again, will leave the sign indeterminate.

Share this post


Link to post
Share on other sites
quote:
Perhaps the result could be stored in a larger data type

int a = ??, b = ??;
long int x;

x = a*b;

if (x == 0){
// one of the values is zero
} else if (x > 0) {
// same sign
} else {
// different sign
}


George D. Filiotis
Are you in support of the ban of Dihydrogen Monoxide? You should be!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
  
if ((a^b)<0)
//different sign

else
//same sign


Now those can be of any type and there''s no need for sizeof thingys, bitshifting nor multiplications. (can''t believe no one suggested this yet)

Share this post


Link to post
Share on other sites
That doesn''t work anon. eg:

a=2
b=-2

a^b=1/(2^2)=1/4 >0

I have a feeling the a^b approach can work somehow though.
hmm...

Frank

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
frank, ^ is XOR operator, not a power operator. XOR stands for exclusive or.

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

The result of xor is 1 only if the two bits are different.

Share this post


Link to post
Share on other sites

  • Advertisement