Rational numbers

Martin Manns mmanns at gmx.net
Fri Feb 23 23:42:00 EST 2007


On Fri, 23 Feb 2007 20:20:12 -0300
"Gabriel Genellina" <gagsl-py at yahoo.com.ar> wrote:

> En Fri, 23 Feb 2007 12:35:19 -0300, Martin Manns <mmanns at gmx.de>
> escribió:
> 
> > I am starting to use rationals and since I found no batteries
> > included, I tried out the mxNumber package.
> >
> > However, I get strange warnings on comparison operations
> > (which however seem to yield correct results):
> 
> mx.Number.Rational is horribly broken. They break this rule:
> 
> a==b => hash(a)==hash(b)
> 
> so they can'b be used as dictionary keys, by example.
> Try the other packages suggested. I've used clnum without problems.
> 

I would be interested, under which circumstances the rule is broken.
In my (few) tests, hash(a)==hash(b) worked.

But I will definitely try clnum.
However, I already started trying out gmpy (1.1):

$ python
Python 2.4.3 (#1, Jan 13 2007, 20:53:15) 
[GCC 4.1.1 (Gentoo 4.1.1-r3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from gmpy import *
>>> a=mpq(0,1)
>>> a
mpq(0)
>>> b=-50000000000000000000000000000000000000000000000000000000000
>>> a==b
__main__:1: RuntimeWarning: tp_compare didn't return -1, 0 or 1
False
>>>

Looks pretty much the same as mx.Number

Does this warning come up from gmp? Do I have to compile it with
different flags?
Or do both wrappers use the same code?

I would like to encourage the python community (i.e. the guys
maintaining the python docs) to point out a recommended rational
library. In one of the rational PEPs, it is stated that there are
multiple robust rational wrappers for python. But it is not stated,
which ones were thoroughly reviewed.

Martin



More information about the Python-list mailing list