bestFracForMinimumError() is only a small part of a program I wrote 
long ago, called frac.py 
(<http://www.rcblue.com/Python/fracForWeb.py>). I'm trying to rewrite 
it so as to get more precision by using the Decimal module, but am 
getting nowhere. Errors all over the place. Can some kind and 
knowledgeable soul show me how?

========================================================
import decimal
def d(x):
     return decimal.Decimal(str(x))
decimal.getcontext().prec = 40


def bestFracForMinimumError(decimal, minimumError):
     denom = 0
     while True:
         denom += 1
         num = round(decimal * denom)
         error = abs((num / denom - decimal) / decimal) * 100
         if error <= minimumError:
             break
     return int(num), denom, error

dec = .345765988765560057657654654

me = .0000000001

print bestFracForMinimumError(dec, me)

num, denom, error = bestFracForMinimumError(dec, me)

print "%f = %d/%d with %f per cent error" % (dec, num, denom, error)
=====================================================

Thanks,

Dick Moores

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to