Tho*_*hle 2 python floating-point mantissa exponent ieee-754
python frexp和ldexp函数将浮点数拆分为尾数和指数.有人知道这个过程是暴露实际的浮动结构,还是需要python来进行昂贵的对数调用?
Python 2.6的math.frexp只是直接调用底层C库frexp.我们必须假设C库只是直接使用浮动表示的部分,而不是计算是否可用(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.");
Run Code Online (Sandbox Code Playgroud)