[Python-Dev] Changes to decimal.py

Tim Peters tim.peters at gmail.com
Fri Apr 13 23:27:50 CEST 2007


[Raymond Hettinger]
> ...
> Likewise, consider soliciting Tim's input on how to implement the ln()
> operation.  That one will be tricky to get done efficiently and correctly.

One low-effort approach is to use a general root-finding algorithm and
build ln(x) on top of exp() via (numerically) solving the equation
exp(ln(x)) == x for ln(x).  That appears to be what Don Peterson did
in his implementation of transcendental functions for Decimal:

    http://cheeseshop.python.org/pypi/decimalfuncs/1.4

In a bit of disguised form, that appears to be what Brian Beck and
Christopher Hesse also did:

    http://cheeseshop.python.org/pypi/dmath/0.9

The former is GPL-licensed and the latter MIT, so the latter would be
easier to start from for core (re)distribution.

However, the IBM spec requires < 1 ULP worst-case error, and that may
be unreasonably hard to meet with a root-finding approach.  If this
can wait a couple months, I'd be happy to own it.  A possible saving
grace for ln() is that while the mathematical function is one-to-one,
in any fixed precision it's necessarily many-to-one (e.g., log10 of
the representable numbers between 10 and 1e100 must be a representable
number between 1 and 100, and there are a lot more of the former than
of the latter -- many distinct representable numbers must map to the
same representable log).


More information about the Python-Dev mailing list