473,626 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decide if a number is negative or positive?

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
Jan 3 '07 #1
19 11532
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.
Jan 3 '07 #2
In article <Yc************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 3 '07 #3
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

Jan 3 '07 #4
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

Jan 3 '07 #5
Richard Tobin wrote:
Richard Heathfield <rj*@see.sig.in validwrote:
>>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>
Jan 3 '07 #6
"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.hbecaus e it is a catch all for the residue of what didn't
fit in some neat niche. And <math.hshould 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 "endinadnes s". I don't have any
particularly helpful hints on that problem. Study and practice, practice,
practice AFAIK.
Jan 3 '07 #7
"Johs" <sd*@sdf.comwro te in message news:en******** **@news.net.uni-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.
Jan 3 '07 #8
In article <45************ ***@yahoo.com>,
CBFalconer <cb********@mai neline.netwrote :
>>>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
implementation s 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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 3 '07 #9
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: <mailto:Th***** ********@gmail. com>

Jan 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5375
by: hasanainf | last post by:
Hi all, I have two querys QueryPurchased ProductID Location TotPcs Prod1 Loc1 100 Prod2 Loc1 50 Prod3 Loc1 150 Prod3 Loc3 150
5
6112
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive and negative quantities . Can any one tell me what are the things i should bear in mind .
17
4014
by: jake1138 | last post by:
Here is a function I have to get a number at the end of a string. I'm posting this in case it proves helpful to someone. Comments are welcome. int getnum(char *str) { char buffer; char *buf = buffer; int i;
13
12304
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist. Thanks ever so much in advance
10
2400
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
43
6554
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than positive integers or a decimal point. For example: Enter amount: and was capturing the float varialbe as in: scanf ("%f", &myVar)
19
3593
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
73
3181
by: cesco | last post by:
I have to generate a list of N random numbers (integer) whose sum is equal to M. If, for example, I have to generate 5 random numbers whose sum is 50 a possible solution could be . Is there a simple pattern or function in Python to accomplish that? Thanks and regards Francesco
63
5646
by: deepak | last post by:
Hi, Can someone give the standard function which can create positive integer value in C? Thanks, Deepak
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.