Skip to content

Commit cfc2986

Browse files
committed
Issue #9165: Add math.isfinite and cmath.isfinite.
1 parent cc37e66 commit cfc2986

7 files changed

Lines changed: 61 additions & 0 deletions

File tree

Doc/library/cmath.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ Hyperbolic functions
187187
Classification functions
188188
------------------------
189189

190+
.. function:: isfinite(x)
191+
192+
Return ``True`` if both the real and imaginary parts of *x* are finite,
193+
and ``False`` otherwise.
194+
195+
190196
.. function:: isinf(x)
191197

192198
Return *True* if the real or the imaginary part of x is positive

Doc/library/math.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ Number-theoretic and representation functions
9797
<http://code.activestate.com/recipes/393090/>`_\.
9898

9999

100+
.. function:: isfinite(x)
101+
102+
Return ``True`` if *x* is neither an infinity nor a NaN, and
103+
``False`` otherwise. (Note that ``0.0`` *is* considered finite.)
104+
105+
100106
.. function:: isinf(x)
101107

102108
Check if the float *x* is positive or negative infinity.

Lib/test/test_cmath.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,15 @@ def test_rect(self):
442442
self.assertCEqual(rect(1, pi/2), (0, 1.))
443443
self.assertCEqual(rect(1, -pi/2), (0, -1.))
444444

445+
def test_isfinite(self):
446+
real_vals = [float('-inf'), -2.3, -0.0,
447+
0.0, 2.3, float('inf'), float('nan')]
448+
for x in real_vals:
449+
for y in real_vals:
450+
z = complex(x, y)
451+
self.assertEquals(cmath.isfinite(z),
452+
math.isfinite(x) and math.isfinite(y))
453+
445454
def test_isnan(self):
446455
self.assertFalse(cmath.isnan(1))
447456
self.assertFalse(cmath.isnan(1j))

Lib/test/test_math.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,15 @@ class TestNoTrunc(object):
915915
self.assertRaises(TypeError, math.trunc, 1, 2)
916916
self.assertRaises(TypeError, math.trunc, TestNoTrunc())
917917

918+
def testIsfinite(self):
919+
self.assertTrue(math.isfinite(0.0))
920+
self.assertTrue(math.isfinite(-0.0))
921+
self.assertTrue(math.isfinite(1.0))
922+
self.assertTrue(math.isfinite(-1.0))
923+
self.assertFalse(math.isfinite(float("nan")))
924+
self.assertFalse(math.isfinite(float("inf")))
925+
self.assertFalse(math.isfinite(float("-inf")))
926+
918927
def testIsnan(self):
919928
self.assertTrue(math.isnan(float("nan")))
920929
self.assertTrue(math.isnan(float("inf")* 0.))

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,9 @@ Library
14151415
Extension Modules
14161416
-----------------
14171417

1418+
- Issue #9165: Add new functions math.isfinite and cmath.isfinite, to
1419+
accompany existing isinf and isnan functions.
1420+
14181421
- Issue #1578269: Implement os.symlink for Windows 6.0+. Patch by
14191422
Jason R. Coombs
14201423

Modules/cmathmodule.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,19 @@ PyDoc_STRVAR(cmath_rect_doc,
10231023
"rect(r, phi) -> z: complex\n\n\
10241024
Convert from polar coordinates to rectangular coordinates.");
10251025

1026+
static PyObject *
1027+
cmath_isfinite(PyObject *self, PyObject *args)
1028+
{
1029+
Py_complex z;
1030+
if (!PyArg_ParseTuple(args, "D:isfinite", &z))
1031+
return NULL;
1032+
return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag));
1033+
}
1034+
1035+
PyDoc_STRVAR(cmath_isfinite_doc,
1036+
"isfinite(z) -> bool\n\
1037+
Return True if both the real and imaginary parts of z are finite, else False.");
1038+
10261039
static PyObject *
10271040
cmath_isnan(PyObject *self, PyObject *args)
10281041
{
@@ -1065,6 +1078,7 @@ static PyMethodDef cmath_methods[] = {
10651078
{"cos", cmath_cos, METH_VARARGS, c_cos_doc},
10661079
{"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
10671080
{"exp", cmath_exp, METH_VARARGS, c_exp_doc},
1081+
{"isfinite", cmath_isfinite, METH_VARARGS, cmath_isfinite_doc},
10681082
{"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc},
10691083
{"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc},
10701084
{"log", cmath_log, METH_VARARGS, cmath_log_doc},

Modules/mathmodule.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,19 @@ PyDoc_STRVAR(math_radians_doc,
18171817
"radians(x)\n\n\
18181818
Convert angle x from degrees to radians.");
18191819

1820+
static PyObject *
1821+
math_isfinite(PyObject *self, PyObject *arg)
1822+
{
1823+
double x = PyFloat_AsDouble(arg);
1824+
if (x == -1.0 && PyErr_Occurred())
1825+
return NULL;
1826+
return PyBool_FromLong((long)Py_IS_FINITE(x));
1827+
}
1828+
1829+
PyDoc_STRVAR(math_isfinite_doc,
1830+
"isfinite(x) -> bool\n\n\
1831+
Check if float x is finite (not an infinity or NaN).");
1832+
18201833
static PyObject *
18211834
math_isnan(PyObject *self, PyObject *arg)
18221835
{
@@ -1868,6 +1881,7 @@ static PyMethodDef math_methods[] = {
18681881
{"fsum", math_fsum, METH_O, math_fsum_doc},
18691882
{"gamma", math_gamma, METH_O, math_gamma_doc},
18701883
{"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
1884+
{"isfinite", math_isfinite, METH_O, math_isfinite_doc},
18711885
{"isinf", math_isinf, METH_O, math_isinf_doc},
18721886
{"isnan", math_isnan, METH_O, math_isnan_doc},
18731887
{"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},

0 commit comments

Comments
 (0)