[Python-Dev] Rational approximation methods

Scott David Daniels Scott.Daniels at Acm.Org
Sat Jan 19 23:35:50 CET 2008


Jeffrey Yasskin wrote:
> The second returns the simplest rational within some distance. For
> instance, it'll prefer 22/7 over 333/106 if both are close enough. We
> might call it .simplest_within() for now. This seems useful for
> converting from float and displaying results to users, where we prefer
> readability over accuracy or have reason to believe that a simpler
> fraction might be more correct.
You can use a mediant walk to get to two surrounding fractions w/ some
limit, and then return the pair to let the caller choose.

See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52317
for an imperfect implementation.



More information about the Python-Dev mailing list