Where in the python source code is math.exp() defined?

依然范特西╮ 提交于 2019-11-29 23:27:53

问题


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


回答1:


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:

  • http://www.netlib.org/fdlibm/e_exp.c

or you could refer to this implementation instead:

  • http://fossies.org/dox/glibc-2.17/sysdeps_2ieee754_2ldbl-128ibm_2e__expl_8c_source.html#l00135


来源:https://stackoverflow.com/questions/15321570/where-in-the-python-source-code-is-math-exp-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!