summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-07-11 17:38:24 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-07-11 17:38:24 (GMT)
commit8e0c9968731ac7f1d94bddff348544c70833b52b (patch)
treeaf0d4866d6dbae9e9b9003435956c6f2f7141dea /Modules
parent3e7428995f9d09493640326104c70a26c7cdab07 (diff)
downloadcpython-8e0c9968731ac7f1d94bddff348544c70833b52b.zip
cpython-8e0c9968731ac7f1d94bddff348544c70833b52b.tar.gz
cpython-8e0c9968731ac7f1d94bddff348544c70833b52b.tar.bz2
Issue #9165: Add math.isfinite and cmath.isfinite.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/cmathmodule.c14
-rw-r--r--Modules/mathmodule.c14
2 files changed, 28 insertions, 0 deletions
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index 2af2e53..986b241 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -1024,6 +1024,19 @@ PyDoc_STRVAR(cmath_rect_doc,
Convert from polar coordinates to rectangular coordinates.");
static PyObject *
+cmath_isfinite(PyObject *self, PyObject *args)
+{
+ Py_complex z;
+ if (!PyArg_ParseTuple(args, "D:isfinite", &z))
+ return NULL;
+ return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag));
+}
+
+PyDoc_STRVAR(cmath_isfinite_doc,
+"isfinite(z) -> bool\n\
+Return True if both the real and imaginary parts of z are finite, else False.");
+
+static PyObject *
cmath_isnan(PyObject *self, PyObject *args)
{
Py_complex z;
@@ -1065,6 +1078,7 @@ static PyMethodDef cmath_methods[] = {
{"cos", cmath_cos, METH_VARARGS, c_cos_doc},
{"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
{"exp", cmath_exp, METH_VARARGS, c_exp_doc},
+ {"isfinite", cmath_isfinite, METH_VARARGS, cmath_isfinite_doc},
{"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc},
{"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc},
{"log", cmath_log, METH_VARARGS, cmath_log_doc},
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 7f9372a..152788f 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -1818,6 +1818,19 @@ PyDoc_STRVAR(math_radians_doc,
Convert angle x from degrees to radians.");
static PyObject *
+math_isfinite(PyObject *self, PyObject *arg)
+{
+ double x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_FINITE(x));
+}
+
+PyDoc_STRVAR(math_isfinite_doc,
+"isfinite(x) -> bool\n\n\
+Check if float x is finite (not an infinity or NaN).");
+
+static PyObject *
math_isnan(PyObject *self, PyObject *arg)
{
double x = PyFloat_AsDouble(arg);
@@ -1868,6 +1881,7 @@ static PyMethodDef math_methods[] = {
{"fsum", math_fsum, METH_O, math_fsum_doc},
{"gamma", math_gamma, METH_O, math_gamma_doc},
{"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
+ {"isfinite", math_isfinite, METH_O, math_isfinite_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},