I want to known how to implement the math.exp()
function in python. Where in the Python source code is math.exp()
defined?

- 36,322
- 27
- 84
- 93

- 165
- 1
- 2
- 10
-
1@PaulC: you're too helpful ! You're just encouraging people to get SOers to use a search engine on their behalf. – High Performance Mark Mar 10 '13 at 11:16
-
@PaulC Tks. I had search on google before ask this question. I could not find the math.exp() function source code. so... – Yuansheng liu Mar 10 '13 at 11:25
-
1This one is actually hard to find, but it would have helped if you showed where you looked an failed to find it. It's defined in C; the entry for the Python structure is in the [`mathmodule.c` file](http://hg.python.org/cpython/file/tip/Modules/mathmodule.c#l1561). – Martijn Pieters Mar 10 '13 at 11:43
-
@PaulC: Right, now tell me *where in the Python source* `math.exp` is defined. At what line number? What file? – Martijn Pieters Mar 10 '13 at 11:47
-
1A [macro defines `exp`](http://hg.python.org/cpython/file/tip/Modules/mathmodule.c#l837) as an application of the [C `exp` function](http://www.manpagez.com/man/3/exp/) wrapped by the [`mathmodule.c`-defined `math_1` function](http://hg.python.org/cpython/file/tip/Modules/mathmodule.c#l652). The implementation of the C function depends on your platform libraries. – Martijn Pieters Mar 10 '13 at 11:49
-
@MartijnPieters Looks like I just learnt something also. Tnx. I had not considered platform issues. Voted to reopen. – Paul Collingwood Mar 10 '13 at 12:01
-
@MartijnPieters Thanks very much. You let me deeply understand this. – Yuansheng liu Mar 10 '13 at 12:29
-
1@MartijnPieters: why did you provide an answer in the comments section? The information you provided is a perfectly acceptable answer. – Bryan Oakley Mar 10 '13 at 12:44
-
@BryanOakley: because the question was closed until I edited it and voted to reopen. I was typing up an expanded version of my comments when it was reopened when neoneo jumped in with his. – Martijn Pieters Mar 10 '13 at 12:49
1 Answers
This one is a little tricky.
The math
module is implemented in a C module, mathmodule.c
. At the end of that file there is a specific Python library structure that defines exp
as implemented by math_exp
:
static PyMethodDef math_methods[] = {
# ...
{"exp", math_exp, METH_O, math_exp_doc},
but math_exp
itself is actually defined via the FUNC1
macro, using the math_1
wrapper function to call the C library exp
function with some error handling and type conversion:
FUNC1(exp, exp, 1,
"exp(x)\n\nReturn e raised to the power of x.")
However, the C function implementation itself is entirely platform dependent, and on modern hardware is usually taken care of in hardware. You would have to turn to a software version of the function to find a starting point to implement this in Python.
You could use the fdlibm
implementation as such a starting point, perhaps, here is a link to the exp
function implementation in C from that library:
or you could refer to this implementation instead:

- 1,048,767
- 296
- 4,058
- 3,343
-
Most hardware (x86 for example) doesn't do `exp` in hardware as a single instruction. Some legacy 32-bit x86 math libraries might have used x87 instructions for it, but even that's just microcoded, not truly dedicated hardware. Normally you stuff the integer part of the exponent into the exponent field of a `float`, and use a polynomial approximation to `exp` over the fractional part. – Peter Cordes May 22 '20 at 22:34