473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Negative integers

I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried

abs(x) / x == abs(y) / y

but that fails when one of the numbers is 0. I'm sure that there is
an easy way to do this. Any suggestions?

Thanks
Aug 20 '08 #1
10 1969
johnewing wrote:
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried

abs(x) / x == abs(y) / y

but that fails when one of the numbers is 0. I'm sure that there is
an easy way to do this. Any suggestions?
(a < 0) == (b < 0)

</F>

Aug 20 '08 #2
On Wed, 20 Aug 2008 14:38:11 -0700, johnewing wrote:
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried

abs(x) / x == abs(y) / y

but that fails when one of the numbers is 0. I'm sure that there is an
easy way to do this. Any suggestions?

Thanks
Don't resort to inline tricks too soon. Write your program in a very
straightforward way (perhaps paying attention to algorithm choice or a
functional style), and only start optimizing if it's too slow. And if it
is too slow, you probably should look at pyrex or similar before
resorting to unusual python code.

Part of writing straightforwardly, means having a function or object that
encapsulates each decision the program makes - so that if that decision
later needs to be changed, it can be changed in one place.

I've been frequenting comp.unix.shell lately, and the group is plagued
with weird little tricks - EG, using : as an alias for true, 1 as an
alias for print, etc. I'd rather not see comp.lang.python turned into
that sort of group. (That said, I program in bash frequently - by choice)

$ cat /tmp/sign
#!/usr/bin/env python

def sign(x):
return cmp(x,0)

print sign(-5)
print sign(0)
print sign(33)

if sign(-5) == sign(-2):
print 'Same sign'

Aug 20 '08 #3
johnewing wrote:
but that fails when one of the numbers is 0. I'm sure that there is
an easy way to do this. Any suggestions?
For the not-so-distant future:

Python 2.6 and 3.0 have a new function "copysign" in the math module. I
added it during the revamp of the math module. copysign(x, y) returns x
as float with the sign of y. It works also works for ints, longs and
floats as y. copysign() is the most reliable and fastest way to
distinguish -0.0 from +0.0.
>>from math import copysign
copysign(1.0, 23)
1.0
>>copysign(1.0, -42)
-1.0
>>copysign(1.0, -0.0)
-1.0
>>copysign(1.0, +0.0)
1.0
>>-0.0 == +0.0
True

Christian

Aug 20 '08 #4
nntpman68 wrote:
johnewing wrote:
>I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried

abs(x) / x == abs(y) / y

but that fails when one of the numbers is 0. I'm sure that there is
an easy way to do this. Any suggestions?

Thanks

Hm,
It seems my previous reply got lost.
if a*b 0:
print "same sign"
else
print "different sign"

Looks good at first, but -5 * 0 == +7 * 0.

~Ethan~
Aug 20 '08 #5
On Aug 21, 7:46 am, Fredrik Lundh <fred...@pythonware.comwrote:
johnewing wrote:
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried
abs(x) / x == abs(y) / y
but that fails when one of the numbers is 0. I'm sure that there is
an easy way to do this. Any suggestions?

(a < 0) == (b < 0)
That supposes that the OP understands "sign" to mean "the sign bit".
Another possibility is the sgn/sign/signum function (http://
en.wikipedia.org/wiki/Sign_function). In that case the answer would be
cmp(a, 0) == cmp(b, 0) -- with one big caveat:

Although cmp appears to return only -1, 0, or +1, it is documented to
return "negative", "zero" or "positive".
>>help(cmp)
Help on built-in function cmp in module __builtin__:

cmp(...)
cmp(x, y) -integer

Return negative if x<y, zero if x==y, positive if x>y.
>>cmp(10, 90)
-1

Perhaps safer and better documentation to define your own sign and
samesign:

sign = lambda x: x < 0
or
sign = lambda x: -1 if x < 0 else 0 if x == 0 else 1
samesign = lambda a, b: sign(a) == sign(b)

Cheers,
John
Aug 20 '08 #6
On Wed, Aug 20, 2008 at 02:38:11PM -0700, johnewing wrote:
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried

abs(x) / x == abs(y) / y
Zero is a problem, no matter how you slice it. Zero can be considered
positive or negative (mathematically, 0 = -0).

If you want zero to be treated always as positive, you can write this:

def same_sign(a, b):
return (abs(a) == a) == (abs(b) == b)

If you want to respect zero's duplicitous nature, you have to write it
like this:

def same_sign(a, b):
if a == 0 or b == 0:
return True
return (abs(a) == a) == (abs(b) == b)

The first version *almost* works for the duplicitous zero:
>>sign(-0, 1)
True
>>sign(0, 1)
True
>>sign(0, -1)
False

Close, but no cigar.

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIrKbzdjdlQoHP510RAuRFAKCayUZPcbsyvGfe7EwHLg Z+EXtfcQCgozYK
7fhrw8i5SqHFiPkCU0OI/8Y=
=r1dA
-----END PGP SIGNATURE-----

Aug 20 '08 #7
I managed to change my dataflow so that an earlier test rules out the
possibility of a zero there. Still, thank you for the fast answers,
this is my first time using the forum. Hopefully I will be able to be
on the question answering end before too long.

thanks again,
john
Aug 20 '08 #8
On Aug 21, 1:30*am, Ethan Furman <et...@stoneleaf.uswrote:
nntpman68 wrote:
johnewing wrote:
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). *I have tried
abs(x) / x == abs(y) / y
but that fails when one of the numbers is 0. *I'm sure that there is
an easy way to do this. *Any suggestions?
Thanks

*Hm,
*>
*>
*It seems my previous reply got lost.
*>
*>
*if a*b 0:
** * print "same sign"
*else
** * print "different sign"
*>
*>

Looks good at first, but -5 * 0 == +7 * 0.
Except that zero is neither negative nor positive, mathematically, so
any answer here is correct.

Eli

http://mathforum.org/library/drmath/view/58735.html
http://en.wikipedia.org/wiki/Negativ...gative_numbers
Aug 21 '08 #9
Derek Martin wrote:
Zero is a problem, no matter how you slice it.
I definitely agree with that. Depends on the the real problem that is
behind the OP:s question.
Zero can be considered
positive or negative (mathematically, 0 = -0).
I've read quite a few articles written by mathematicians and
mathematically oriented engineers, and my impression is that most of
them, and therefore me included, use those words with the following meaning:

Positive: >0
Negative: <0

When zero is to be included, the following terms are used:

Non-negative: >=0
Non-positive: <=0

Using this convention, zero is neither positive nor negative. Wikipedia
seems to agree with that:

http://en.wikipedia.org/wiki/Positive_number

/MiO
Aug 21 '08 #10
On 20 Aug., 23:38, johnewing <john....@gmail.comwrote:
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). *I have tried

abs(x) / x == abs(y) / y

but that fails when one of the numbers is 0. *I'm sure that there is
an easy way to do this. *Any suggestions?

Thanks
Multiply with x and y and you get
abs(x) * y == abs(y) * x
which avoids zero division.
but testing x*y 0 is easier.

Greetings, Uwe
Aug 21 '08 #11

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

Similar topics

0
by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP 4.3.1 on both systems. Here is my program,...
2
by: MLH | last post by:
Am using code below to display memory status. Problem with 4th and 5th ones (dwTotalPageFile and dwAvailPageFile). They show up as NEGATIVE. Why might that be? 'xxxxxxxxxxxxxxxBEGIN...
5
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...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
3
by: Steven D'Aprano | last post by:
Problem: I have an application where I need to print integers differently depending on whether they are positive or negative. To be more specific, I have to print something that looks like: ...
19
by: Johs | last post by:
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
1
by: twocansam1 | last post by:
I am writing a routine to check for bad or no input, How can I check for blanks or spaces and negative numers?? negative numbers and spaces just cause the program to lock up. They don't get...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.