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 )
How do I check if 2 numbers are oppositely signed in C++
In C:
In C++:
That should work, but it isn''t tested.
#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 MarteeOriginal 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
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement