[Tutor] need help with 3.1's fraction module

Richard D. Moores rdmoores at gmail.com
Wed Jan 6 23:41:54 CET 2010


On Wed, Jan 6, 2010 at 11:59, bob gailer <bgailer at gmail.com> wrote:
> Fraction(str(random())).limit_denominator(100)

Thanks!

So here's what I wanted to do, or its beginning:

======================
from fractions import Fraction
from random import random

r = random()
print(r)

f = str(Fraction(str(r)).limit_denominator(100))
print(f)

n, d = f.split('/')
num, denom = int(n), int(d)
fraction = num/denom

error = abs(r - fraction)/r

print(error*100, "percent")
========================
Several outputs are

0.744931449894
73/98
0.00449581106515 percent

and

0.139041980606
5/36
0.110104672465 percent

and

0.577608333884
41/71
0.0248516413283 percent

Dick


More information about the Tutor mailing list