Negative integers

eliben eliben at gmail.com
Thu Aug 21 00:13:43 EDT 2008


On Aug 21, 1:30 am, Ethan Furman <et... at 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



More information about the Python-list mailing list