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 19 11509
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 <Yc******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>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 floatingpoint 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 nonsense like +0 or 0.
HTH

Regards
Taran
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) ... cosigned ...
(fiddle with >=, <= to taste) isn't good enough. Or
if (a < 0 == b < 0) ... cosigned ...

Chris "hopefully not Pyecroft" Dollin
"The path to the web becomes deeper and wider"  October Project
Richard Tobin wrote:
Richard Heathfield <rj*@see.sig.invalidwrote:
>>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 floatingpoint 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
nonportable.

Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
"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.hbecause 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 "endinadness". I don't have any
particularly helpful hints on that problem. Study and practice, practice,
practice AFAIK.
"Johs" <sd*@sdf.comwrote in message news:en**********@news.net.unic.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.
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.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 floatingpoint 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.
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 "nonnegative" (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 email me at: <mailto:Th*************@gmail.com>
CBFalconer <cb********@yahoo.comwrites:
Richard Tobin wrote:
>Richard Heathfield <rj*@see.sig.invalidwrote:
>>>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 floatingpoint 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
nonportable.
Apart from not being, as far as I can see, relevant to the point, I
haven't heard anything about allbitszero being universally a
representation for 0.0. It wasn't until relatively recently
(postC99) that the committee decided that allbitszero is always a
representation of 0 for integer types; could that be what you're
thinking of?

Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"David T. Ashley" <dt*@e3ft.comwrites:
"Johs" <sd*@sdf.comwrote in message news:en**********@news.net.unic.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.
This is an obscure way to go about it. The standard doesn't require a
2's complement representation for signed integers, though it's almost
universal these days. The fact that you talk about testing only the
upper byte or word of each operand tells me you're thinking in terms
of microoptimization, which is usually a bad idea *unless* you've
confirmed that the code in question is a significant performance
bottleneck.
(Personally, I wouldn't try to use bitwise operations on signed
integers; I've never bothered to learn how they behave. If I ever
need to know, I'll look it up.)
Also, the OP didn't say he's working with integers.
Just write the code as clearly as possible, such as:
(a >= 0 && b >= 0)  (a < 0 && b < 0)
or even
(a >= 0) == (b >= 0)

Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:
"David T. Ashley" <dt*@e3ft.comwrites:
>"Johs" <sd*@sdf.comwrote in message news:en**********@news.net.unic.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.
This is an obscure way to go about it. The standard doesn't require a
2's complement representation for signed integers, though it's almost
universal these days. The fact that you talk about testing only the
upper byte or word of each operand tells me you're thinking in terms
of microoptimization, which is usually a bad idea *unless* you've
confirmed that the code in question is a significant performance
bottleneck.
(Personally, I wouldn't try to use bitwise operations on signed
integers; I've never bothered to learn how they behave. If I ever
need to know, I'll look it up.)
Also, the OP didn't say he's working with integers.
Just write the code as clearly as possible, such as:
(a >= 0 && b >= 0)  (a < 0 && b < 0)
or even
(a >= 0) == (b >= 0)
or even
!(a < 0) ^ !(b < 0)

Joe Wright
"Everything should be made as simple as possible, but not simpler."
 Albert Einstein 
Joe Wright wrote:
>
Keith Thompson wrote:
Just write the code as clearly as possible, such as:
(a >= 0 && b >= 0)  (a < 0 && b < 0)
or even
(a >= 0) == (b >= 0)
or even
!(a < 0) ^ !(b < 0)
Not even.
He said "as clearly as possible" for a reason.
/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
int a, b;
puts("/* BEGIN new.c output */\n");
for (a = 1; a != 2; ++a) {
for (b = 1; b != 2; ++b) {
printf("a is %d\nb is %d\n", a, b);
printf("(a >= 0 && b >= 0)  (a < 0 && b < 0) is %d\n",
(a >= 0 && b >= 0)  (a < 0 && b < 0));
printf("(a >= 0) == (b >= 0) is %d\n",
(a >= 0) == (b >= 0));
printf("!(a < 0) ^ !(b < 0) is %d\n",
!(a < 0) ^ !(b < 0));
putchar('\n');
}
}
puts("/* END new.c output */");
return 0;
}
/* END new.c */

pete
Joe Wright wrote:
Keith Thompson wrote:
Just write the code as clearly as possible, such as:
(a >= 0 && b >= 0)  (a < 0 && b < 0)
or even
(a >= 0) == (b >= 0)
or even
!(a < 0) ^ !(b < 0)
Do you mean !( !(a < 0) ^ !(b < 0) ) ?
Spiros Bousbouras wrote:
>
Joe Wright wrote:
Keith Thompson wrote:
Just write the code as clearly as possible, such as:
>
(a >= 0 && b >= 0)  (a < 0 && b < 0)
>
or even
>
(a >= 0) == (b >= 0)
>
or even
!(a < 0) ^ !(b < 0)
Do you mean !( !(a < 0) ^ !(b < 0) ) ?
!(a < 0) ^ (b < 0) would work, but
(a >= 0) == (b >= 0) is really a better way to write it.

pete
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"David T. Ashley" <dt*@e3ft.comwrites:
>"Johs" <sd*@sdf.comwrote in message news:en**********@news.net.unic.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.
This is an obscure way to go about it. The standard doesn't require a
2's complement representation for signed integers, though it's almost
universal these days. The fact that you talk about testing only the
upper byte or word of each operand tells me you're thinking in terms
of microoptimization, which is usually a bad idea *unless* you've
confirmed that the code in question is a significant performance
bottleneck.
(Personally, I wouldn't try to use bitwise operations on signed
integers; I've never bothered to learn how they behave. If I ever
need to know, I'll look it up.)
Also, the OP didn't say he's working with integers.
Just write the code as clearly as possible, such as:
(a >= 0 && b >= 0)  (a < 0 && b < 0)
or even
(a >= 0) == (b >= 0)
I agree with everything you wrote about obscurity. I'll just make the
observation that compilers are notoriously bad at certain types of
comparisons.
A plain vanilla comparison (a < b) is generally done well.
However, with bitfields in particular, I've seen some really bad code. For
example, on the compiler I'm using presently (with bitfields of size 1), the
comparison:
(x.bf1 == x.bf2)
results in a huge bulk of code as the bitfields are converted into integers
and then compared.
I've actually found it is cheaper to write:
((x.bf1 && x.bf2)  (!x.bf1 && !x.bf2))
I do agree with you in principle about microoptimization. You are right.
Keith Thompson wrote:
>
.... snip ...
>
Apart from not being, as far as I can see, relevant to the point,
I haven't heard anything about allbitszero being universally a
representation for 0.0. It wasn't until relatively recently
(postC99) that the committee decided that allbitszero is
always a representation of 0 for integer types; could that be
what you're thinking of?
Probably.

Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
>And if either of the values may be a floatingpoint 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 nonportable.
On the other hand, many implementations (including all the IEEE
floatingpoint ones) have at least two distinct representations of
zero, at least one of which isn't allbitszero (although the other
one probably is allbitszero).
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?
Anyways here's the code, not tested though:
if ( (a* b) >= 0 )
Will cause trouble if a*b overflows. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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

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

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

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

by: chanma 
last post by:
code1:var x=0xf0000000;
alert(x);
output:4026531840
code2:var b=0xf<<28;
alert(b);
output:268435456

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

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

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

by: deepak 
last post by:
Hi,
Can someone give the standard function which can create positive
integer value in C?
Thanks,
Deepak

by: CloudSolutions 
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

by: Faith0G 
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

by: ryjfgjl 
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...

by: ryjfgjl 
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...

by: emmanuelkatto 
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel

by: BarryA 
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

by: nemocccc 
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

by: Sonnysonu 
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by columnwise with in the specific length.
suppose the i have to...

by: Hystou 
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
 