2

I am trying to find the actual implementation of math.pow in Python3 (3.7.3) on Ubuntu 18.04 LTS (Bionic Beaver).

The Python doc says the math module

provides access to the mathematical functions defined by the C standard.

This post says the math module

is usually included in OS distributions. ... 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.

So, there are 3 possible implementations Python, Ubuntu, microprocessors.

I searched python math module source code, and then google got me the mathmodule.c.

I didn't find the definition, declaration, wrapper, or implementation of math.pow in this mathmodule.c file.

I noticed that the mathmodule.c includes some headers.

#include "Python.h"
#include "_math.h"

#include "clinic/mathmodule.c.h"

So, I searched them respectively, and got

"Python.h" is a "meta-include" file

Include nearly all Python header files

"_math.h" seems to be for Hyperbolic functions

the clue in "mathmodule.c.h" ends at math_pow_impl

static PyObject *
math_pow_impl(PyObject *module, double x, double y);

math_pow_impl is implemented at mathmodule.c

static PyObject *
math_pow_impl(PyObject *module, double x, double y)
/*[clinic end generated code: output=fff93e65abccd6b0 input=c26f1f6075088bfd]*/
{
    double r;
    int odd_y;

...

errno = 0;
PyFPE_START_PROTECT("in math_pow", return 0);
r = pow(x, y);
PyFPE_END_PROTECT(r);

r = pow(x, y); seems to be the key, although mathmodule.c only uses the function rather than implementing this function, what is the next step I could try?

PS:

I also searched on my ubuntu (/usr/include/math.h) and got no result contains "pow".

3
  • It's probably just a wrapper for the C pow() function. Since you're using Ubuntu, look it up in the glibc source. There are assorted cpu-specific versions using hardware instructions and a generic ieee754 compatible version in there to dig out.
    – Shawn
    Sep 9, 2019 at 7:25
  • Also, the implementation of math_pow_impl() is in that mathmodule.c file you linked to...
    – Shawn
    Sep 9, 2019 at 7:33
  • The function pow is a standard C library function — you probably have to look for that in the source for the GNU C Library, therefore. Sep 9, 2019 at 7:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.