GMPY compare warning

Mensanator mensanator at aol.compost
Sat Nov 29 00:17:40 EST 2003


In the program that follows, I get the following warning message:

collatz_.py:37: RuntimeWarning: tp_compare didn't return -1, 0 or 1
  while b>1:

In this case, b is a gmpy.mpz number. The program operates as it should,
which I guess is why it's a warning. Is there some reason why I shouldn't
use gmpy numbers with relational operators? Is this warning something
that can safely be ignored?




import sys
import time
import gmpy

i = long(sys.argv[1])

n = 2**(i) - 1

r1 = 0
r2 = 0
t0 = time.time()

a = n

while a>1:
        z = divmod(a,2)
        if z[1]==0:
                a = z[0]
                r1 += 1
        else:
                a = a*3 + 1
                r2 += 1

t1 = time.time()

print "r1",r1,"r2",r2,"in",t1-t0,"seconds\n"


b = gmpy.mpz(n)

r1 = 0
r2 = 0
t0 = time.time()

while b>1:
        z = gmpy.scan1(b)
        if z==0:
                b = b*3 + 1
                r2 += 1
        else:
                b = b/(2**z)
                r1 += z

t1 = time.time()

print "r1",r1,"r2",r2,"in",t1-t0,"seconds\n"

"""
C:\Python23\user>python collatz_.py 1000
r1 7841 r2 4316 in 0.109000086784 seconds
r1 7841 r2 4316 in 0.375 seconds

C:\Python23\user>python collatz_.py 10000
r1 86278 r2 48126 in 6.42199993134 seconds
r1 86278 r2 48126 in 4.85900008678 seconds

C:\Python23\user>python collatz_.py 100000
r1 863323 r2 481603 in 604.766000032 seconds
r1 863323 r2 481603 in 121.859999895 seconds
"""




--
Mensanator
Ace of Clubs




More information about the Python-list mailing list