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

Started by
39 comments, last by executor_2k2 22 years, 2 months ago
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 )

"If patience is a virtue, and ignorance is bliss, you can have a pretty good life if you're stupid and willing to wait"
Advertisement
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.

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"
"If patience is a virtue, and ignorance is bliss, you can have a pretty good life if you're stupid and willing to wait"
quote:Original post by Tac-Tics two zeros technically have the same sign

Not necessarily.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
quote:Original post by Martee
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.
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
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.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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.
If you divide, you run the risk of integer underflow, which, again, will leave the sign indeterminate.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement