473,320 Members | 1,921 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,320 software developers and data experts.

cmp() on integers - is there guarantee of returning only +-1 or 0?

doc says that it must be > 0, or < 0, but it seems that
it returns +1 or -1. Can it be reliably used to get the sign of x:
cmp(x, 0) like pascal Sign() function does? I mean, I'm
pretty sure that it can be used, but is it mentioned somewhere
in language spec, or it may be implementation defined?
If so, any other simple means of _reliably_ getting the sign?
Mar 19 '06 #1
8 1177
"Dmitry Anikin" <an*****************@vstu.ru> writes:
doc says that it must be > 0, or < 0, but it seems that
it returns +1 or -1. Can it be reliably used to get the sign of x:
cmp(x, 0) like pascal Sign() function does?
The doc says (http://docs.python.org/lib/built-in-funcs.html):

cmp(x,y)
Compare the two objects x and y and return an integer according
to the outcome. The return value is negative if x < y, zero if
x == y and strictly positive if x > y.
I mean, I'm pretty sure that it can be used, but is it mentioned
somewhere in language spec, or it may be implementation defined?
The doc doesn't specify that it returns -1/0/+1 so by definition
it's implementation defined.
If so, any other simple means of _reliably_ getting the sign?


Ehh, every way I see offhand is at least a little bit messy.
Mar 19 '06 #2
It is depending on the classes you try to compare, and on how the
comparison functions (see
http://docs.python.org/ref/customization.html) are implemented in
these, see example below:

py> class wrong(object):
.... def __init__(self, x):
.... self.x = x
.... def __cmp__(self, other):
.... if self.x < other.x:
.... return -1
.... else:
.... return 1
....
py> class right(object):
.... def __init__(self, x):
.... self.x = x
.... def __cmp__(self, other):
.... if self.x < other.x:
.... return -1
.... elif self.x > other.x:
.... return 1
.... else:
.... return 0
....
py> w1 = wrong(1)
py> w2 = wrong(1)
py> cmp(w1, w2)
1
py>
py> r1 = right(1)
py> r2 = right(1)
py> cmp(r1, r2)
0

Mar 19 '06 #3

Dmitry> doc says that it must be > 0, or < 0, but it seems that it
Dmitry> returns +1 or -1. Can it be reliably used to get the sign of x:
Dmitry> cmp(x, 0) like pascal Sign() function does?

Why not

def sign(n):
return n and n/abs(n) or 0

Skip
Mar 19 '06 #4

def sign(n):
return n and n/abs(n) or 0

Whoops... Make that

def sign(n):
return n and n/abs(n) or 1

Skip
Mar 19 '06 #5
<sk**@pobox.com> wrote:
Dmitry> doc says that it must be > 0, or < 0, but it seems that it
Dmitry> returns +1 or -1. Can it be reliably used to get the sign of x:
Dmitry> cmp(x, 0) like pascal Sign() function does?

Why not

def sign(n):
return n and n/abs(n) or 0


If you assume n is a number, the 'or 0' appears redundant (if you don't
so assume, then the abs(n) and the division are doubtful;-).
Alex
Mar 19 '06 #6
<sk**@pobox.com> wrote:
def sign(n):
return n and n/abs(n) or 0

Whoops... Make that

def sign(n):
return n and n/abs(n) or 1


Uh? I thought part of the specs was that sign(0) is 0...
Alex
Mar 19 '06 #7
Alex Martelli wrote:
<sk**@pobox.com> wrote:
Why not

def sign(n):
return n and n/abs(n) or 0


If you assume n is a number, the 'or 0' appears redundant (if you don't
so assume, then the abs(n) and the division are doubtful;-).


Without the 'or 0' it is also more consistent with the behavior of your
function that the sign of a float is also a float.

One issue is that division should be normally avoided because it is a
costly operation and can lead to rounding errors. Maybe performance is
is not a problem for ints and floats, but if you are planning to use the
function on long ints, I would consider using something like:

def sign(x): return x>0 and 1 or x<0 and -1 or 0

def sign(x): return (x>0)-(x<0)

-- Christoph
Mar 20 '06 #8
Christoph Zwerschke <ci**@online.de> writes:
def sign(x): return (x>0)-(x<0)


Cute, I'll try to remember that one.
Mar 20 '06 #9

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

Similar topics

3
by: Ivan Van Laningham | last post by:
Hi All-- I noticed recently that a few of the jpgs from my digital cameras have developed bitrot. Not a real problem, because the cameras are CD Mavicas, and I can simply copy the original from...
39
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are...
87
by: j0mbolar | last post by:
I've read in the standard that addresses basically can't be interpreted as integers. If they can, it is implementation defined behavior. However, if they can't be viewed as integers in any sense...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.