I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this?
Johs
解决方案Johs said:
I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this?
How about zero, which is neither positive or negative?
With that caveat, consider this:
#define SAME_SIGN(a, b) ((((a)<0)==((b)<0))&&(((a)>0)==((b)>0)))
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
In article ,
Richard Heathfield
>I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this? >How about zero, which is neither positive or negative?
And if either of the values may be a floating-point number, you need
to consider how you want -0 (which is distinct from +0 in some
floating point systems) to behave.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Johs wrote:
I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this?
Johs
I am not really sure if there is an inbuilt function or not. AFAIK it
would take less time to code it than to find that special function if
it was available.
Anyways here''s the code, not tested though:
/* product of two positive or two negative numbers is always positive
* and equality with zero takes care of the special case 0.
*/
if ( (a* b) >= 0 )
{
/* the numbers are either both positive or both negative
*/
}
This will also remove the the conflict between +0 and -0, which ever
type of signed 0 it is, they both must be of the same sign.
Sometimes we have to live with non-sense like +0 or -0.
HTH
--
Regards
Taran