在python源代码中的哪个位置定义了math.exp()? [英] Where in the python source code is math.exp() defined?

查看:178
本文介绍了在python源代码中的哪个位置定义了math.exp()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆