python - Difference between math.exp(2) and math.e**2 -


this question has answer here:

while programming noticed difference between result of math.exp(2) , math.e**2. can see below, difference not arise when calculating e^1.

not being experienced programmer, wondered why differs? assume has rounding up. python docs math.exp(x) return e**x, appears not precisely correct. how come math.exp(x) operation differs math.e**x?

>>> math.exp(1) 2.718281828459045 >>> math.e**1 2.718281828459045 >>> math.exp(1)==math.e**1 true >>> math.exp(2) 7.38905609893065 >>> math.e**2 7.3890560989306495 >>> math.exp(2)==math.e**2 false >>> math.exp(100) 2.6881171418161356e+43 >>> math.e**100 2.6881171418161212e+43 >>> math.exp(100)==math.e**100 false 

it's different due differences in implementation of functions. neither 1 perfect due nature of floating point.

the ** operator implemented in floatobject.c , contains lot of special cases, none of them invoked here, reaches standard c pow function. math.pow function same thing.

the exp function straightforward wrapper around c function of same name, defined macro in mathmodule.c.

as happens, exp more precise (both of results match, within precision allowed floating point, high-precision answers calculated in bc). because internally pow, calculating extended-precision logarithm of double-precision e value pass first argument, smaller 1.


the fundamental issue math.e, number you're calculating power of, is:

2.718281828459045 09079559829... 

whereas real value of e is

2.718281828459045 23536028747... 

and error compounded when use pow or **, whereas may not (or may using higher precision value internally) if you're using exp due details of algorithms uses.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -