问题
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()
?
回答1:
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.
otool -tvV /usr/lib/system/libsystem_m.dylib
回答2:
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.
回答3:
A simple grep over the code would have helped:
http://svn.python.org/view/python/trunk/Modules/cmathmodule.c?revision=76978&view=markup
回答4:
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:
print x**0.5
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:
print(x**0.5)
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:
sqrt(x):
return x**0.5
For other roots such as cubic root etc, you could use a function like this:
root(x, root):
return x**root
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! :)
来源:https://stackoverflow.com/questions/5476189/where-can-i-inspect-pythons-math-functions