It's different due to differences in the implementation of the functions. Neither one is perfect due to the nature of floating point.

The ** operator is implemented in floatobject.c and contains a lot of special cases, but none of them are invoked here, so it ultimately reaches the standard C pow function. The math.pow function ultimately does the same thing.

The exp function is a straightforward wrapper around the C function of the same name, defined with a macro in mathmodule.c.

As it happens, exp is more precise (both of the results match, to within the precision allowed by floating point, high-precision answers I calculated in bc). Most likely it is because internally to pow, it is calculating the extended-precision logarithm of the double-precision e value you pass as the first argument, which is slightly smaller than 1.


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

2.718281828459045 09079559829...

Whereas the real value of e is

2.718281828459045 23536028747...

And this error is compounded when you use pow or **, whereas it may not be (or may be using a higher precision value internally) if you're using exp due to the details of the algorithms it uses.

Answer from Random832 on Stack Overflow
Top answer
1 of 2
8

It's different due to differences in the implementation of the functions. Neither one is perfect due to the nature of floating point.

The ** operator is implemented in floatobject.c and contains a lot of special cases, but none of them are invoked here, so it ultimately reaches the standard C pow function. The math.pow function ultimately does the same thing.

The exp function is a straightforward wrapper around the C function of the same name, defined with a macro in mathmodule.c.

As it happens, exp is more precise (both of the results match, to within the precision allowed by floating point, high-precision answers I calculated in bc). Most likely it is because internally to pow, it is calculating the extended-precision logarithm of the double-precision e value you pass as the first argument, which is slightly smaller than 1.


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

2.718281828459045 09079559829...

Whereas the real value of e is

2.718281828459045 23536028747...

And this error is compounded when you use pow or **, whereas it may not be (or may be using a higher precision value internally) if you're using exp due to the details of the algorithms it uses.

2 of 2
7

exp(x) is implemented at a much lower level than e**x. It is essentially a wrapper for a function in libc. Said function is probably (at some level) using the Taylor series expansion to calculate the value directly (or possibly some other mathematical method, I'm not sure).

On the other hand, e**x is taking a number and raising it to a power. This is an entirely different strategy, and probably less precise in most circumstances. Raising numbers to powers is difficult to do precisely.

🌐
Tutorialspoint
tutorialspoint.com › home › python › python number exponential function
Python Number Exponential Function
February 21, 2009 - The Python math.exp() method is used to compute the Euler's number 'e' raised to the power of a numeric value. The Eulers number is simply defined as a mathematical expression that is usually used as the base of natural logarithms.
🌐
Greenteapress
greenteapress.com › wp › think-python-2e
Think Python, 2nd edition – Green Tea Press
The second edition uses Python 3. If you are using Python 2, you might want to use the first edition, which is here.
🌐
Python
python.org › download › releases › 2.0
Python 2.0 | Python.org
The final version of Python 2.0 is available for download now. Windows installer. To install Python, Windows users need only download the Windows installer and run it. ... If you're running Windows 95, 98, ME, NT or 2000, all you need is the Windows installer. It includes Python, Tcl/Tk, and the documentation in HTML format. Simply download the installer and run it. Some browsers remove the ".exe...
🌐
DataCamp
datacamp.com › tutorial › exponents-in-python
Exponents in Python: A Comprehensive Guide for Beginners | DataCamp
November 25, 2024 - base = 5 exponent = -2 print(base**exponent) # 0.04 base = 25 exponent = -5 print(base**exponent) # 1.024e-07 · Raising a number to a negative exponent is equivalent to taking the reciprocal of the number raised to the positive exponent. In Python, the ** operator returns a ZeroDivisionError if you raise 0.0 to a negative power because any number divided by zero is undefined.
🌐
Real Python
realpython.com › python-numbers
Numbers in Python – Real Python
April 1, 2023 - Python also uses E notation to display large floating-point numbers: ... The float 200000000000000000.0 gets displayed as 2e+17. The + sign indicates that the exponent 17 is a positive number.
🌐
Educative
educative.io › answers › calculating-the-exponential-value-in-python
Calculating the exponential value in Python
We can use math.e to get the value of e in Python. The known number of digits of e keeps changing, and as of 2010, the known number of computable digits is trillions of digits of e. We can display any number raised to the power of 2 in the following way:
🌐
Altcademy
altcademy.com › blog › how-to-use-e-in-python
How to use e in Python
August 31, 2023 - The Magic of e in Python Understanding the Concept of e Before we delve into how to use e in Python, let's first understand what e is. The term e is a famous mathematical constant, similar to pi (π), but it has its unique applications. It's approximately equal to 2.
🌐
W3Schools
w3schools.com › python › ref_math_exp.asp
Python math.exp() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Find elsewhere
🌐
Manning
livebook.manning.com › concept › python › e-notation
e-notation in python - liveBook · Manning
liveBooks are enhanced books. They add narration, interactive exercises, code execution, and other features to eBooks.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Power and Logarithmic Functions in Python: exp, log, log10, log2 | note.nkmk.me
August 10, 2023 - The natural logarithm, which uses a base of e and is represented in mathematics by "log" or "ln", can be calculated using math.log(x). ... The common logarithm, which uses a base of 10, can be calculated with math.log10(x). This gives a more accurate value than math.log(x, 10). math.log10() — Mathematical functions — Python 3.11.4 documentation ... The binary logarithm, which uses a base of 2, can be calculated with math.log2(x).
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › float › scientific.html
e | E (scientific notation) — Python Reference (The Right Way) 0.1 documentation
[0-9].E[0-9] >>> 1.1 1.1 >>> 1.1e0 1.1 # 1.1 * 10**0 >>> 1.1e1 11.0 # 1.1 * 10**1 >>> 1.1e2 110.0 # 1.1 * 10**2 >>> 1.1e3 1100.0 # 1.1 * 10**3 >>> 8e-2 0.08 # 8 * 10**-2
🌐
Python-future
python-future.org › compatible_idioms.html
Cheat Sheet: Writing Python 2-3 compatible code — Python-Future documentation
except ValueError as e: ... ... Short integers are gone in Python 3 and long has become int (without the trailing L in the repr). # Python 2 only k = 9223372036854775808L # Python 2 and 3: k = 9223372036854775808
🌐
Python
docs.python.org › 2 › tutorial
The Python Tutorial — Python 2.7.18 documentation
This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used feature. Instead, it introduces many of Python’s most noteworthy features, and will give you a good idea of the language’s flavor and style. After reading it, you will be able to read and write Python modules and programs, and you will be ready to learn more about the various Python library modules described in The Python Standard Library. The Glossary is also worth going through. 1. Whetting Your Appetite · 2.
🌐
W3Schools
w3schools.com › python › ref_math_e.asp
Python math.e Constant
Python Examples Python Compiler ... Bootcamp Python Certificate Python Training ... The math.e constant returns the Eular's number: 2.718281828459045....
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .exp()
Python:NumPy | Math Methods | .exp() | Codecademy
April 17, 2025 - The exponential function, np.exp(x), returns e^x, where e is Euler’s number with an approximate value of 2.71828. As a part of NumPy, a widely used library for numerical computing in Python, this function is particularly useful in scientific computations where exponential functions are common.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
Return the mantissa and exponent of x as the pair (m, e). m is a float and e is an integer such that x == m * 2**e exactly. If x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1. This is used to “pick apart” the internal representation ...
🌐
Python
docs.python.org › 2
Python 2.7.18 documentation
Welcome! This is the documentation for Python 2.7.18.