e vs exp()?

Terry Reedy tjreedy at udel.edu
Fri Sep 20 00:42:43 EDT 2002


"Buckshot" <c6re at sdsumus.sdstate.edu> wrote in message
news:3a838998.0209191933.1f22e977 at posting.google.com...
> 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
> Type "help", "copyright", "credits" or "license" for more
information.
> >>> from math import *
> >>> e**2
> 7.3890560989306522
> >>> exp(2)
> 7.3890560989306504

ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on
Python 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import *
>>> e**2
7.3890560989306495 # same as below to within roundoff, but not quite
same...
>>> exp(2)
7.3890560989306504 # matched your result exactly

The last decimal digit reported for floats is almost always flakey.
Both round to 7.3......650, so I suspect this is correct and e**2 on
your machine is not.

On the hypothesis that x**y is calculated as exp(y*log(x)), I tried
>>> exp(2*log(e))
7.3890560989306504
I expected this to end in ..495 so I am puzzled a bit also.  How is
e**x being calculated to give a different result?  Answer in the
Python source maybe, or in C library source.  In some other
situations, hypothesis seems correct
>>> pi
3.1415926535897931
>>> sq = sqrt(2)
>>> sq
1.4142135623730951
>>> pi**sq
5.0474972673709111
>>> exp(sq*log(pi))
5.0474972673709111
but not in all
>>> sq**pi
2.9706864235520198
>>> exp(pi*log(sq))
2.9706864235520194

Using 2.0 gives same result as 2:
>>> e**2.0
7.3890560989306495
>>> exp(2.0)
7.3890560989306504
>>> log(e)
1.0

Perhaps your system does not have e quite as correct:
>>> e
2.7182818284590451

Terry J. Reedy









More information about the Python-list mailing list