Decimal and Exponentiation

Dan Bishop danb_83 at yahoo.com
Fri May 19 20:09:10 EDT 2006


Tim Peters wrote:
...
> Wait <0.3 wink>.  Python's Decimal module intends to be a faithful
> implementation of IBM's proposed standard for decimal arithmetic:
>
>     http://www2.hursley.ibm.com/decimal/
>
> Last December, ln, log10, exp, and exponentiation to non-integral
> powers were added to the proposed standard, but nobody yet has written
> implementation code for Python's  module.  [Python-Dev:  somebody
> wants to volunteer for this :-)]

Here's a quick-and-dirty exp function:


def exp(x):
   """
   Return e raised to the power of x.
   """
   if x < 0:
      return 1 / exp(-x)
   partial_sum = term = 1
   i = 1
   while True:
      term *= x / i
      new_sum = partial_sum + term
      if new_sum == partial_sum:
         return new_sum
      partial_sum = new_sum
      i += 1




More information about the Python-list mailing list