[Tutor] Need help with rewriting script to use Decimal module

Terry Carroll carroll at tjc.com
Thu Jan 4 17:20:41 CET 2007


On Wed, 3 Jan 2007, Dick Moores wrote:

> Terry, I just noticed that farey(0.36, 10) returns (1, 3), a pretty 
> big miss, IMO. The correct fraction with smallest error and maximum 
> denominator of 10 is 3/8, which I'm proud to say my klunky frac.py 
> (<http://www.rcblue.com/Python/fracForWeb.py>) produces.

Dick, good catch.  Consider adding a comment to the Cookbook page pointing
out that the method sometimes does not produce the incorrect answer, 
including for that input.

I note that the web page for the recipe says only that its produces "a 
rational," not necessarily the closest rational, but the description in 
the book says it's the closest:

  You have a number v (of almost any type) and need to find a rational
  number (in reduced form) that is as close to v as possible but with a 
  denominator no larger than a prescribed value. (Recipe 18.13)




More information about the Tutor mailing list