查看python模块的math源代码_在python源代码中的哪里定义了math.exp()?

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

解决方案

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值