PEP 240 scares me

Scottie me at nospam.net
Sun Apr 1 18:04:12 EDT 2001


Sorry, I let this go before finishing editting.

If converting to rationals scares you, try this based on James Farey's
"Farey fractions.

 def Farey(v, lim):
   '''Convert v to a rational with denom <= lim

   Named after James Farey, an English surveyor.
   No error checking on args -- v >= 0, lim = max denominator,
   results are (numerator, denominator) in lowest terms.
   Think of (1,0) as infinity.
   '''
   lower, upper = (0L,1L), (1L,0L)
   while 1:
     mediant = (lower[0] + upper[0]), (lower[1]+upper[1])
     if v * mediant[1] > mediant[0]:
         if lim < mediant[1]: return upper
         lower = mediant
     else:
         if lim < mediant[1]: return lower
         upper = mediant






More information about the Python-list mailing list