e vs exp()?
Terry Reedy
tjreedy at udel.edu
Fri Sep 20 17:56:53 EDT 2002
"Fernando Pérez" <fperez528 at yahoo.com> wrote in message
news:amflhl$3eb$1 at peabody.colorado.edu...
> 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
> > Type "help", "copyright", "credits" or "license" for more
information.
> >>>> from math import *
> >>>> e**2
> > 7.3890560989306522
> >>>> exp(2)
> > 7.3890560989306504
>
> As someone already told you, exp(2) gives a more accurate answer.
The reason
> is, I think, the following: e**2 simply squares e,
Thanks. That seems to solve the particular problem posed:
>>> e*e
7.3890560989306495
>>> e**2
7.3890560989306495
>>> pow(e,2)
7.3890560989306495
>>> exp(2)
7.3890560989306504
Perhaps x**int is special-cased. But following must have a different
explanation:
>>> e**2.1
8.1661699125676499
>>> pow(e,2.1)
8.1661699125676499
>>> exp(2.1)
8.1661699125676517
At least two of us expected x**y == pow(x,y) == exp(y*log(x)) (a
standard method, and the only one I could find in Abrahamowitz and
Stegun, Handbook of Mathematical Functions) but there must be a more
direct method being used at least in some C libraries now.
Terry J. Reedy
More information about the Python-list
mailing list