在哪里可以检查Python的数学函数? [英] Where can I inspect Python's math functions?

查看:85
本文介绍了在哪里可以检查Python的数学函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看Python计算平方根的方式,所以我试图找到math.sqrt()的定义,但是我在任何地方都找不到.我看过_math.cmathmodule.c和其他地方.

I would like to look at the way Python does computes square roots, so I tried to find the definition for math.sqrt(), but I can't find it anywhere. I have looked in _math.c, mathmodule.c, and elsewhere.

我知道python使用C的数学函数,但是这些是在Python发行版中的某个地方,还是它们链接到其他地方的代码?我正在使用Mac OS X.

I know that python uses C's math functions, but are these somewhere in the Python distribution, or are they linked to code elsewhere? I am using Mac OS X.

math.sqrt()中的算法在哪里?

推荐答案

这取决于实现. CPython使用标准C库中的数学函数. Jython最有可能使用Java的数学方法.依此类推.

It depends on the implementation. CPython is using math functions from the standard C library. Jython is most likely using Java's math methods. And so on.

实际上,Python与数学函数的实际实现无关.那些与IEEE 754更为相关,IEEE 754几乎专门用于表示当今计算机中的浮点数.

In fact, Python has nothing to do with the actual implementation of math functions. Those are more related to IEEE 754 which is used almost exclusively to represent floating point numbers in computers nowadays.

无论如何,就CPython而言,其math模块只是对C函数的一个薄包装(

Anyway, speaking in terms of CPython, its math module is just a thin wrapper over C functions (prooflink, at the bottom of the page). The C functions are implemented as part of the standard C library. It is usually included in OS distributions and it is most likely distributed in binary form, without sources. Note also that many microprocessors have specialised instructions for some of these operations, and your compiler may well make use of those rather than jumping to the implementation in the C library.

我无法告诉您系统上标准C库中使用的确切算法. 此处说明.

I can't tell you the exact algorithm which is used in the standard C library on your system. Some of the possible algorithms are explained here.

在OS X的特定情况下,数学函数位于libSystem.dylib中,不幸的是,该函数不是开放源代码(只有

In the specific case of OS X, the math functions live in libSystem.dylib, which unfortunately is not Open Source (there is only stub code available on Apple's Open Source site). You can however disassemble it if you are interested - on current systems, try e.g.

otool -tvV /usr/lib/system/libsystem_m.dylib

这篇关于在哪里可以检查Python的数学函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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