mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Added math.isinf() and math.isnan()
This commit is contained in:
parent
ae04c3356e
commit
e2ca4245c9
4 changed files with 69 additions and 6 deletions
|
|
@ -66,6 +66,23 @@ Number-theoretic and representation functions:
|
||||||
apart" the internal representation of a float in a portable way.
|
apart" the internal representation of a float in a portable way.
|
||||||
|
|
||||||
|
|
||||||
|
.. function:: isinf(x)
|
||||||
|
|
||||||
|
Checks if the float *x* is positive or negative infinite.
|
||||||
|
|
||||||
|
..versionadded:: 2.6
|
||||||
|
|
||||||
|
|
||||||
|
.. function:: isnan(x)
|
||||||
|
|
||||||
|
Checks if the float *x* is a NaN (not a number). NaNs are part of the
|
||||||
|
IEEE 754 standards. Operation like but not limited to ``inf * 0``,
|
||||||
|
``inf / inf`` or any operation involving a NaN, e.g. ``nan * 1``, return
|
||||||
|
a NaN.
|
||||||
|
|
||||||
|
..versionadded:: 2.6
|
||||||
|
|
||||||
|
|
||||||
.. function:: ldexp(x, i)
|
.. function:: ldexp(x, i)
|
||||||
|
|
||||||
Return ``x * (2**i)``. This is essentially the inverse of function
|
Return ``x * (2**i)``. This is essentially the inverse of function
|
||||||
|
|
|
||||||
|
|
@ -229,6 +229,22 @@ class MathTests(unittest.TestCase):
|
||||||
self.ftest('tanh(0)', math.tanh(0), 0)
|
self.ftest('tanh(0)', math.tanh(0), 0)
|
||||||
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
|
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
|
||||||
|
|
||||||
|
def testIsnan(self):
|
||||||
|
self.assert_(math.isnan(float("nan")))
|
||||||
|
self.assert_(math.isnan(float("inf")* 0.))
|
||||||
|
self.failIf(math.isnan(float("inf")))
|
||||||
|
self.failIf(math.isnan(0.))
|
||||||
|
self.failIf(math.isnan(1.))
|
||||||
|
|
||||||
|
def testIsinf(self):
|
||||||
|
self.assert_(math.isinf(float("inf")))
|
||||||
|
self.assert_(math.isinf(float("-inf")))
|
||||||
|
self.assert_(math.isinf(1E400))
|
||||||
|
self.assert_(math.isinf(-1E400))
|
||||||
|
self.failIf(math.isinf(float("nan")))
|
||||||
|
self.failIf(math.isinf(0.))
|
||||||
|
self.failIf(math.isinf(1.))
|
||||||
|
|
||||||
# RED_FLAG 16-Oct-2000 Tim
|
# RED_FLAG 16-Oct-2000 Tim
|
||||||
# While 2.0 is more consistent about exceptions than previous releases, it
|
# While 2.0 is more consistent about exceptions than previous releases, it
|
||||||
# still fails this part of the test on some platforms. For now, we only
|
# still fails this part of the test on some platforms. For now, we only
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #1640: Added math.isinf() and math.isnan() functions.
|
||||||
|
|
||||||
- Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj
|
- Issue #1726: Remove Python/atof.c from PCBuild/pythoncore.vcproj
|
||||||
|
|
||||||
- Removed PCbuild8/ directory and added a new build directory for VS 2005
|
- Removed PCbuild8/ directory and added a new build directory for VS 2005
|
||||||
|
|
|
||||||
|
|
@ -315,9 +315,8 @@ math_log10(PyObject *self, PyObject *arg)
|
||||||
PyDoc_STRVAR(math_log10_doc,
|
PyDoc_STRVAR(math_log10_doc,
|
||||||
"log10(x) -> the base 10 logarithm of x.");
|
"log10(x) -> the base 10 logarithm of x.");
|
||||||
|
|
||||||
/* XXX(nnorwitz): Should we use the platform M_PI or something more accurate
|
static const double degToRad = Py_MATH_PI / 180.0;
|
||||||
like: 3.14159265358979323846264338327950288 */
|
static const double radToDeg = 180.0 / Py_MATH_PI;
|
||||||
static const double degToRad = 3.141592653589793238462643383 / 180.0;
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
math_degrees(PyObject *self, PyObject *arg)
|
math_degrees(PyObject *self, PyObject *arg)
|
||||||
|
|
@ -325,7 +324,7 @@ math_degrees(PyObject *self, PyObject *arg)
|
||||||
double x = PyFloat_AsDouble(arg);
|
double x = PyFloat_AsDouble(arg);
|
||||||
if (x == -1.0 && PyErr_Occurred())
|
if (x == -1.0 && PyErr_Occurred())
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyFloat_FromDouble(x / degToRad);
|
return PyFloat_FromDouble(x * radToDeg);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(math_degrees_doc,
|
PyDoc_STRVAR(math_degrees_doc,
|
||||||
|
|
@ -343,6 +342,33 @@ math_radians(PyObject *self, PyObject *arg)
|
||||||
PyDoc_STRVAR(math_radians_doc,
|
PyDoc_STRVAR(math_radians_doc,
|
||||||
"radians(x) -> converts angle x from degrees to radians");
|
"radians(x) -> converts angle x from degrees to radians");
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
math_isnan(PyObject *self, PyObject *arg)
|
||||||
|
{
|
||||||
|
double x = PyFloat_AsDouble(arg);
|
||||||
|
if (x == -1.0 && PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
return PyBool_FromLong((long)Py_IS_NAN(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(math_isnan_doc,
|
||||||
|
"isnan(x) -> bool\n\
|
||||||
|
Checks if float x is not a number (NaN)");
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
math_isinf(PyObject *self, PyObject *arg)
|
||||||
|
{
|
||||||
|
double x = PyFloat_AsDouble(arg);
|
||||||
|
if (x == -1.0 && PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
return PyBool_FromLong((long)Py_IS_INFINITY(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(math_isinf_doc,
|
||||||
|
"isinf(x) -> bool\n\
|
||||||
|
Checks if float x is infinite (positive or negative)");
|
||||||
|
|
||||||
|
|
||||||
static PyMethodDef math_methods[] = {
|
static PyMethodDef math_methods[] = {
|
||||||
{"acos", math_acos, METH_O, math_acos_doc},
|
{"acos", math_acos, METH_O, math_acos_doc},
|
||||||
{"asin", math_asin, METH_O, math_asin_doc},
|
{"asin", math_asin, METH_O, math_asin_doc},
|
||||||
|
|
@ -358,6 +384,8 @@ static PyMethodDef math_methods[] = {
|
||||||
{"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
|
{"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
|
||||||
{"frexp", math_frexp, METH_O, math_frexp_doc},
|
{"frexp", math_frexp, METH_O, math_frexp_doc},
|
||||||
{"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
|
{"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
|
||||||
|
{"isinf", math_isinf, METH_O, math_isinf_doc},
|
||||||
|
{"isnan", math_isnan, METH_O, math_isnan_doc},
|
||||||
{"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
|
{"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
|
||||||
{"log", math_log, METH_VARARGS, math_log_doc},
|
{"log", math_log, METH_VARARGS, math_log_doc},
|
||||||
{"log10", math_log10, METH_O, math_log10_doc},
|
{"log10", math_log10, METH_O, math_log10_doc},
|
||||||
|
|
@ -389,13 +417,13 @@ initmath(void)
|
||||||
if (d == NULL)
|
if (d == NULL)
|
||||||
goto finally;
|
goto finally;
|
||||||
|
|
||||||
if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
|
if (!(v = PyFloat_FromDouble(Py_MATH_PI)))
|
||||||
goto finally;
|
goto finally;
|
||||||
if (PyDict_SetItemString(d, "pi", v) < 0)
|
if (PyDict_SetItemString(d, "pi", v) < 0)
|
||||||
goto finally;
|
goto finally;
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
|
|
||||||
if (!(v = PyFloat_FromDouble(exp(1.0))))
|
if (!(v = PyFloat_FromDouble(Py_MATH_E)))
|
||||||
goto finally;
|
goto finally;
|
||||||
if (PyDict_SetItemString(d, "e", v) < 0)
|
if (PyDict_SetItemString(d, "e", v) < 0)
|
||||||
goto finally;
|
goto finally;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue