| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3e74289 commit 8e0c996
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -187,6 +187,12 @@ Hyperbolic functions | |||
| 187 | 187 | Classification functions | |
| 188 | 188 | ------------------------ | |
| 189 | 189 | ||
| 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 | + | ||
| 190 | 196 | .. function:: isinf(x) | |
| 191 | 197 | ||
| 192 | 198 | Return *True* if the real or the imaginary part of x is positive | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,6 +97,12 @@ Number-theoretic and representation functions | |||
| 97 | 97 | <http://code.activestate.com/recipes/393090/>`_\. | |
| 98 | 98 | ||
| 99 | 99 | ||
| 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 | + | ||
| 100 | 106 | .. function:: isinf(x) | |
| 101 | 107 | ||
| 102 | 108 | Check if the float *x* is positive or negative infinity. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -442,6 +442,15 @@ def test_rect(self): | |||
| 442 | 442 | self.assertCEqual(rect(1, pi/2), (0, 1.)) | |
| 443 | 443 | self.assertCEqual(rect(1, -pi/2), (0, -1.)) | |
| 444 | 444 | ||
| 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 | + | ||
| 445 | 454 | def test_isnan(self): | |
| 446 | 455 | self.assertFalse(cmath.isnan(1)) | |
| 447 | 456 | self.assertFalse(cmath.isnan(1j)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -915,6 +915,15 @@ class TestNoTrunc(object): | |||
| 915 | 915 | self.assertRaises(TypeError, math.trunc, 1, 2) | |
| 916 | 916 | self.assertRaises(TypeError, math.trunc, TestNoTrunc()) | |
| 917 | 917 | ||
| 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 | + | ||
| 918 | 927 | def testIsnan(self): | |
| 919 | 928 | self.assertTrue(math.isnan(float("nan"))) | |
| 920 | 929 | self.assertTrue(math.isnan(float("inf")* 0.)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1415,6 +1415,9 @@ Library | |||
| 1415 | 1415 | Extension Modules | |
| 1416 | 1416 | ----------------- | |
| 1417 | 1417 | ||
| 1418 | + - Issue #9165: Add new functions math.isfinite and cmath.isfinite, to | ||
| 1419 | + accompany existing isinf and isnan functions. | ||
| 1420 | + | ||
| 1418 | 1421 | - Issue #1578269: Implement os.symlink for Windows 6.0+. Patch by | |
| 1419 | 1422 | Jason R. Coombs | |
| 1420 | 1423 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1023,6 +1023,19 @@ PyDoc_STRVAR(cmath_rect_doc, | |||
| 1023 | 1023 | "rect(r, phi) -> z: complex\n\n\ | |
| 1024 | 1024 | Convert from polar coordinates to rectangular coordinates."); | |
| 1025 | 1025 | ||
| 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 | + | ||
| 1026 | 1039 | static PyObject * | |
| 1027 | 1040 | cmath_isnan(PyObject *self, PyObject *args) | |
| 1028 | 1041 | { | |
@@ -1065,6 +1078,7 @@ static PyMethodDef cmath_methods[] = { | |||
| 1065 | 1078 | {"cos", cmath_cos, METH_VARARGS, c_cos_doc}, | |
| 1066 | 1079 | {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc}, | |
| 1067 | 1080 | {"exp", cmath_exp, METH_VARARGS, c_exp_doc}, | |
| 1081 | + {"isfinite", cmath_isfinite, METH_VARARGS, cmath_isfinite_doc}, | ||
| 1068 | 1082 | {"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc}, | |
| 1069 | 1083 | {"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc}, | |
| 1070 | 1084 | {"log", cmath_log, METH_VARARGS, cmath_log_doc}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1817,6 +1817,19 @@ PyDoc_STRVAR(math_radians_doc, | |||
| 1817 | 1817 | "radians(x)\n\n\ | |
| 1818 | 1818 | Convert angle x from degrees to radians."); | |
| 1819 | 1819 | ||
| 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 | + | ||
| 1820 | 1833 | static PyObject * | |
| 1821 | 1834 | math_isnan(PyObject *self, PyObject *arg) | |
| 1822 | 1835 | { | |
@@ -1868,6 +1881,7 @@ static PyMethodDef math_methods[] = { | |||
| 1868 | 1881 | {"fsum", math_fsum, METH_O, math_fsum_doc}, | |
| 1869 | 1882 | {"gamma", math_gamma, METH_O, math_gamma_doc}, | |
| 1870 | 1883 | {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, | |
| 1884 | + {"isfinite", math_isfinite, METH_O, math_isfinite_doc}, | ||
| 1871 | 1885 | {"isinf", math_isinf, METH_O, math_isinf_doc}, | |
| 1872 | 1886 | {"isnan", math_isnan, METH_O, math_isnan_doc}, | |
| 1873 | 1887 | {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments