在哪里可以检查 Python 的数学函数?
我想看看 Python 计算平方根的方式,所以我试图找到 math.sqrt() 的定义,但我在任何地方都找不到它。我查看了 _math.c
、mathmodule.c
和其他地方。
我知道 python 使用 C 的数学函数,但这些函数是在 Python 发行版中的某个地方,还是链接到其他地方的代码?我使用的是 Mac OS X。
math.sqrt()
中的算法在哪里?
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.
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.
Where is the algorithm in math.sqrt()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于实施。 CPython 使用标准 C 库中的数学函数。 Jython 最有可能使用Java 的数学方法。等等。
事实上,Python 与数学函数的实际实现无关。这些与 IEEE 754 更相关,后者几乎专门用于表示当今计算机中的浮点数。
无论如何,就 CPython 而言,它的 math 模块只是 C 函数的薄包装(验证链接,位于页面底部)。 C 函数作为标准 C 库的一部分实现。它通常包含在操作系统发行版中,并且很可能以二进制形式分发,没有源代码。另请注意,许多微处理器对其中一些操作都有专门的指令,您的编译器很可能会利用这些指令,而不是跳转到 C 库中的实现。
我无法告诉您系统上标准 C 库中使用的确切算法。 此处解释了一些可能的算法。
在 OS X 的特定情况下,数学函数位于
libSystem.dylib
中,不幸的是它不是开源的(只有 Apple 开源网站上提供的存根代码)。但是,如果您有兴趣,您可以拆卸它 - 在当前系统上,尝试例如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.
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.
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.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.
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.有些模块是用 C 编写的,而不是用 python 编写的,因此您将无法找到 .py 文件。对于这些的列表,您可以使用:
import sys
print sys.builtin_module_names
由于它是用 C 编写的,因此您必须在源代码中找到它。如果您已经有源代码,它位于模块目录中。
Some modules are written in C and not in python so you wouldn't be able to find the .py files. For a list of these you can use:
import sys
print sys.builtin_module_names
Since it's written in C you will have to find it in the source code. If you have the source already it's in the modules directory.
我不确定在哪里可以找到 Python 使用的确切算法,但我希望这对您有所帮助。在 Python 中计算平方根的最简单方法是使用 **(幂)运算符。我不知道你对指数做了多少工作,但平方根与取一半的幂相同。因此,如果这是真的,您可以使用:
这会打印您放在 x 位置的任何数字的平方根。当然,如果您使用的是 Python 3,那么您需要将其写为:
这将是创建计算数字平方根的算法的最简单方法。这可以在函数中实现,例如:
对于其他根(例如立方根等),您可以使用如下函数:
并且当您将根数传递到函数中时,使用十进制形式的索引数,例如:
2: 0.5
3: 0.33333333 (重复)
4: 0.25
5: 0.2
我希望你能看到这个模式。我也希望这对您有所帮助! :)
I'm not sure where to find the exact algorithm used by Python, but I hope this helps you. The easiest way to calculate a square root in Python is by using the ** (power) operator. I don't know how much work you have done with indices but square root is the same as putting something to the power of a half. So with that being true you could use:
This prints the square root of whatever number you put in the place of x. Of course, if you are using Python 3 then you will need to write this as:
That would be the easiest way to make an algorithm to calculate the square root of a number. This could be implemented in a function such as:
For other roots such as cubic root etc, you could use a function like this:
And when you are passing the root number into the function use the numbers of the indices in decimal form, for example:
2: 0.5
3: 0.33333333 (recurring)
4: 0.25
5: 0.2
I hope you can see the pattern. I also hope this helped you some! :)