[Python-Dev] Rational approximation methods

Paul Moore p.f.moore at gmail.com
Sun Jan 20 14:28:27 CET 2008


On 19/01/2008, Jeffrey Yasskin <jyasskin at gmail.com> wrote:
> The first returns the closest rational whose denominator is less than
> a given integer.
[...]
> The second returns the simplest rational within some distance.

Both of these are likely to be of limited use. The most common usage I
know of is to make a "sensible" rational from a float (i.e., a DWIM
style conversion 0.1 -> 1/10) or to provide readable output. On the
other hand, both are subtle to implement, so having a standard
implementation saves people having to code (and debug) their own.

I suspect that simplest within a particular distance is the form that
is most often wanted, but I don't have any good evidence for that
feeling. Putting both in might help people to realise that there *is*
a choice.

Given that you have implemented them, I'd say leave them in. And I
like the names trim and approximate, as given in the code you
referenced.

Paul.


More information about the Python-Dev mailing list