e vs exp()?

James J. Besemer jb at cascade-sys.com
Fri Sep 20 00:51:25 EDT 2002

```Buckshot wrote:

>Can anyone here explain why I'm getting different results from
>e**2 and exp(2)?  Which is more accurate?
>
>Python 2.2 (#1, Mar 27 2002, 14:56:58)
>[GCC 2.95.3 20010315 (release)] on sunos5
>
>
>>>>from math import *
>>>>e**2
>>>>
>>>>
>7.3890560989306522
>
>
>>>>exp(2)
>>>>
>>>>
>7.3890560989306504
>
>
Mathematica says e**2 to 30 digits is:

7.3890560989306502 272304274606

in which case the second form would happen to be more accurate.

Since Python math uses your machine's native floating point formats,
precision is necessarily limited and one should not be surprised to
encounter minor errors in the least significant bits or digits.

Although Mathematica recognizes that the two forms are identical, Python
uses different formula for computing the two results.  The fact that the
base is "e" is of no consequence to the computation.  exp() is computed
from C's exp() runtime library routine, whereas e**2 is evaluated using
C's "pow()" function.

In general, I would expect pow() to be less accurate, as pow(x,y)
probably is computed by exp(log(x)*y).

--jb

--
James J. Besemer		503-280-0838 voice
2727 NE Skidmore St.		503-280-0375 fax
Portland, Oregon 97211-6557	mailto:jb at cascade-sys.com