Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Negative integers

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


john.ewi at gmail

Aug 20, 2008, 2:38 PM

Post #1 of 14 (512 views)
Permalink
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
--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Aug 20, 2008, 2:46 PM

Post #2 of 14 (474 views)
Permalink
Re: Negative integers [In reply to]

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>

--
http://mail.python.org/mailman/listinfo/python-list


vlastimil.brom at gmail

Aug 20, 2008, 2:55 PM

Post #3 of 14 (478 views)
Permalink
Re: Negative integers [In reply to]

2008/8/20 johnewing <john.ewi [at] gmail>

> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
What about a straightforward approach like:

(a < 0 and b < 0) or (a >= 0 and b >= 0)

Probably there are some more sophisticated/elegant ... ones.

Vlasta


dstromberglists at gmail

Aug 20, 2008, 3:00 PM

Post #4 of 14 (474 views)
Permalink
Re: Negative integers [In reply to]

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'

--
http://mail.python.org/mailman/listinfo/python-list


news1234 at free

Aug 20, 2008, 3:01 PM

Post #5 of 14 (474 views)
Permalink
Re: Negative integers [In reply to]

Hm,


It seems my previous reply got lost.


if a*b > 0:
print "same sign"
else
print "different sign"


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
--
http://mail.python.org/mailman/listinfo/python-list


news1234 at free

Aug 20, 2008, 3:15 PM

Post #6 of 14 (472 views)
Permalink
Re: Negative integers [In reply to]

Hi,

Dan's solution is probably the clearest.


Just out of curiousity:.

How would you in your program's context like 0 to be treated.

should it be treated as a positive number or should it be treated as
a case part.

my suggestion a*b > 0 for example
wouldn't work if you want 0 to be treated as positive number or if you
compare 0 with 0 and want 'same sign' as answer

bye


N

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
--
http://mail.python.org/mailman/listinfo/python-list


lists at cheimes

Aug 20, 2008, 3:19 PM

Post #7 of 14 (470 views)
Permalink
Re: Negative integers [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


sjmachin at lexicon

Aug 20, 2008, 3:32 PM

Post #8 of 14 (479 views)
Permalink
Re: Negative integers [In reply to]

On Aug 21, 7:46 am, Fredrik Lundh <fred...@pythonware.com> 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?
>
> (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
--
http://mail.python.org/mailman/listinfo/python-list


ethan at stoneleaf

Aug 20, 2008, 4:20 PM

Post #9 of 14 (481 views)
Permalink
Re: Negative integers [In reply to]

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~
--
http://mail.python.org/mailman/listinfo/python-list


code at pizzashack

Aug 20, 2008, 4:21 PM

Post #10 of 14 (463 views)
Permalink
Re: Negative integers [In reply to]

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


john.ewi at gmail

Aug 20, 2008, 4:29 PM

Post #11 of 14 (471 views)
Permalink
Re: Negative integers [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


eliben at gmail

Aug 20, 2008, 9:13 PM

Post #12 of 14 (459 views)
Permalink
Re: Negative integers [In reply to]

On Aug 21, 1:30 am, Ethan Furman <et...@stoneleaf.us> wrote:
> 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/Negative_and_non-negative_numbers
--
http://mail.python.org/mailman/listinfo/python-list


mikael at isy

Aug 21, 2008, 1:06 AM

Post #13 of 14 (457 views)
Permalink
Re: Negative integers [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


rocksportrocker at googlemail

Aug 21, 2008, 1:54 AM

Post #14 of 14 (447 views)
Permalink
Re: Negative integers [In reply to]

On 20 Aug., 23:38, johnewing <john....@gmail.com> 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

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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.