在python源代码中的哪个位置定义了math.exp()? [英] Where in the python source code is math.exp() defined?
问题描述
我想知道如何在python中实现math.exp()
函数. math.exp()
在Python源代码中的何处定义?
I want to known how to implement the math.exp()
function in python. Where in the Python source code is math.exp()
defined?
推荐答案
这有点棘手.
math
模块在C模块 mathmodule.c
中实现.该文件的末尾有一个特定的Python库结构,该结构定义了已实现的 exp
通过math_exp
:
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},
但math_exp
本身实际上是通过 FUNC1
宏,使用 math_1
包装函数调用C库exp
函数,其中包含一些错误处理和类型转换:
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.")
但是,C函数实现本身完全依赖于平台,并且在现代硬件上通常会在硬件中使用 .您必须转向该函数的软件版本才能找到在Python中实现此目标的起点.
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.
您可以将fdlibm
实现用作起点,也许,这是该库中C语言中exp
函数实现的链接:
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:
这篇关于在python源代码中的哪个位置定义了math.exp()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!