在蟒frexp和ldexp功能分割成浮点数尾数和指数。 不要任何人知道,如果这个过程中暴露出的实际浮动结构,或者如果它需要Python做昂贵的对数电话?

有帮助吗?

解决方案

至于速度,这里是一个快速的比较

$ python -m timeit -c 'from math import frexp' 'frexp(1.1)'
100000 loops, best of 3: 3.7 usec per loop

$ python -m timeit -c 'from math import log' 'log(1.1)'
100000 loops, best of 3: 3.7 usec per loop

$ python -m timeit -c 'from math import ldexp' 'ldexp(1.1,2)'
100000 loops, best of 3: 3.5 usec per loop

因此,有没有很多在速度方面的差异在frexplogldexp之间蟒可检测的。不知道,告诉你,虽然执行的东西!

其他提示

2.6的Python的math.frexp只是调用底层的C库frexp直接。我们必须假定C库简单地直接使用浮点表示的部件代替计算如果avaliable(IEEE 754)。

static PyObject *
math_frexp(PyObject *self, PyObject *arg)
{
        int i;
        double x = PyFloat_AsDouble(arg);
        if (x == -1.0 && PyErr_Occurred())
                return NULL;
        /* deal with special cases directly, to sidestep platform
           differences */
        if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
                i = 0;
        }
        else {  
                PyFPE_START_PROTECT("in math_frexp", return 0);
                x = frexp(x, &i);
                PyFPE_END_PROTECT(x);
        }
        return Py_BuildValue("(di)", x, i);
}

PyDoc_STRVAR(math_frexp_doc,
"frexp(x)\n"
"\n"
"Return the mantissa and exponent of x, as pair (m, e).\n"
"m is a float and e is an int, such that x = m * 2.**e.\n"
"If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.");

这是一个问题,你可以很容易地回答自己:

$ python
>>> import math
>>> help(math.frexp)
Help on built-in function frexp in module math:

注意的内置。它在C

>>> import urllib
>>> help(urllib.urlopen)
Help on function urlopen in module urllib:

没有的内置的位置。它是在Python。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top