Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Decide if a number is negative or positive?

Reply
Thread Tools

Decide if a number is negative or positive?

 
 
Johs
Guest
Posts: n/a
 
      01-03-2007
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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2007
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.
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      01-03-2007
In article <(E-Mail Removed)>,
Richard Heathfield <(E-Mail Removed)> 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?


>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.
 
Reply With Quote
 
Taran
Guest
Posts: n/a
 
      01-03-2007
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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      01-03-2007
Taran wrote:

> 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


Or the product can't be represented as a value of the appropriate
kind. This is a Very Bad Way to try and solve the problem.

I don't see why

if (a > 0 && b > 0 || a < 0 && b < 0) ... co-signed ...

(fiddle with >=, <= to taste) isn't good enough. Or

if (a < 0 == b < 0) ... co-signed ...

--
Chris "hopefully not Pyecroft" Dollin
"The path to the web becomes deeper and wider" - October Project

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-03-2007
Richard Tobin wrote:
> Richard Heathfield <(E-Mail Removed)> 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?

>
>> 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.


I believe the standards commitee has found that there are no
implementations extant where insistance on all bits zero will fail
to simulate 0.0, and thus plan to allow that in the future. NULL
is another matter, but usually works. Document any such usage as
non-portable.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      01-03-2007
"Johs" writes:

>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?


No, there is no built in function to do this. The best way, in general, to
answer this kind of thing in the future is to take a good reference, such as
the appendix in K&R, and make some personal notes on the various headers.
This will force you to actually read, rather than skim, the contents. Most
everything will fit into some kind of category, but you really need to
study <stdlib.h> because it is a catch all for the residue of what didn't
fit in some neat niche. And <math.h> should be studied as well.

IMO the people that wrote the standard were pretty careful not to innundate
the thing with drivel. I consider something as pointless when it is trivial
and obvious to do it with the basic language. But everyone has their own
personal "trivial" of course. The "pointless" functions along the lines of
what you asked that comes to mind is abs() and fabs(). Some of the stuff
may be trivial but not obvious, ceil() and floor() in <math.h>come to mind.

But in a practical sense this still leaves the question, OK, it is not
standard but "Does *my* compiler have such a function?" For example, there
might be something special to convert "endinadness". I don't have any
particularly helpful hints on that problem. Study and practice, practice,
practice AFAIK.


 
Reply With Quote
 
David T. Ashley
Guest
Posts: n/a
 
      01-03-2007
"Johs" <(E-Mail Removed)> wrote in message news:enfn79$bmt$(E-Mail Removed)-c.dk...
>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


If they are both signed integers of the same size, the easiest way to test
for both positive or both negative is probably something like:

(a ^ b) >= 0

I'm naturally making the assumption of standard 2's complement
representation (and I haven't seen a machine that doesn't support this, but
I don't know what standards apply).

This assumes that the machine hardware economically fetches an integer in
one instruction. If not, one needs only to test the upper byte or word of
each.


 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      01-03-2007
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?


If by "positive" you really mean "non-negative" (ie: including zero),
then this (*untested*) should work:

#define SAME_SIGN(a,b) ( ((a)<0) == ((b)<0) )

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      01-03-2007
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> 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?


>>> 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.


>I believe the standards commitee has found that there are no
>implementations extant where insistance on all bits zero will fail
>to simulate 0.0, and thus plan to allow that in the future.


I don't see the relevance of this to the question. I was pointing out
that the OP must decide whether he wants to consider -0 and +0 as
having the same sign or not; this has nothing to do with the
representation.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Adding a positive number and a negative number MRAB Python 2 01-31-2009 02:41 AM
Adding a positive number and a negative number EK Python 0 01-30-2009 04:13 AM
How to decide whether it is an English letter or a number? QQ C Programming 10 06-19-2006 08:43 AM
Negative setup and Negative hold prem_eda VHDL 5 10-11-2004 12:14 PM
negative number evaluating greater than string.size() Jason C++ 25 02-22-2004 08:43 PM



Advertisments