summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2015-01-11 11:55:29 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2015-01-11 11:55:29 (GMT)
commita5d0c7c2fdd638d684acdb5391e0987b1e37f56a (patch)
tree90debcd5a43e635d8a0dda8cb69533588c6c513e
parent845b14cc8ef2d95e72c97a788a1ffb31faeaa3a8 (diff)
downloadcpython-a5d0c7c2fdd638d684acdb5391e0987b1e37f56a.zip
cpython-a5d0c7c2fdd638d684acdb5391e0987b1e37f56a.tar.gz
cpython-a5d0c7c2fdd638d684acdb5391e0987b1e37f56a.tar.bz2
Issue #23185: add math.inf and math.nan constants.
-rw-r--r--Doc/library/math.rst16
-rw-r--r--Doc/whatsnew/3.5.rst6
-rw-r--r--Lib/test/test_math.py11
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/mathmodule.c35
5 files changed, 69 insertions, 1 deletions
diff --git a/Doc/library/math.rst b/Doc/library/math.rst
index 3c41672..eda0056 100644
--- a/Doc/library/math.rst
+++ b/Doc/library/math.rst
@@ -383,6 +383,22 @@ Constants
The mathematical constant e = 2.718281..., to available precision.
+.. data:: inf
+
+ A floating-point positive infinity. (For negative infinity, use
+ ``-math.inf``.) Equivalent to the output of ``float('inf')``.
+
+ .. versionadded:: 3.5
+
+
+.. data:: nan
+
+ A floating-point "not a number" (NaN) value. Equivalent to the output of
+ ``float('nan')``.
+
+ .. versionadded:: 3.5
+
+
.. impl-detail::
The :mod:`math` module consists mostly of thin wrappers around the platform C
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
index f3cc514..bea6a8a 100644
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -243,6 +243,12 @@ re
* Now unmatched groups are replaced with empty strings in :func:`re.sub`
and :func:`re.subn`. (Contributed by Serhiy Storchaka in :issue:`1519638`.)
+math
+----
+
+* :data:`math.inf` and :data:`math.nan` constants added. (Contributed by Mark
+ Dickinson in :issue:`23185`.)
+
shutil
------
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index c9f3f16..023dea9 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -983,6 +983,17 @@ class MathTests(unittest.TestCase):
self.assertFalse(math.isinf(0.))
self.assertFalse(math.isinf(1.))
+ @requires_IEEE_754
+ def test_nan_constant(self):
+ self.assertTrue(math.isnan(math.nan))
+
+ @requires_IEEE_754
+ def test_inf_constant(self):
+ self.assertTrue(math.isinf(math.inf))
+ self.assertGreater(math.inf, 0.0)
+ self.assertEqual(math.inf, float("inf"))
+ self.assertEqual(-math.inf, float("-inf"))
+
# RED_FLAG 16-Oct-2000 Tim
# 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
diff --git a/Misc/NEWS b/Misc/NEWS
index 5f550d2..9a10abf 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -203,6 +203,8 @@ Core and Builtins
Library
-------
+- Issue #23185: Add math.inf and math.nan constants.
+
- Issue #23186: Add ssl.SSLObject.shared_ciphers() and
ssl.SSLSocket.shared_ciphers() to fetch the client's list ciphers sent at
handshake.
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index bce3799..0c991cd 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -223,6 +223,35 @@ lanczos_sum(double x)
return num/den;
}
+/* Constant for +infinity, generated in the same way as float('inf'). */
+
+static double
+m_inf(void)
+{
+#ifndef PY_NO_SHORT_FLOAT_REPR
+ return _Py_dg_infinity(0);
+#else
+ return Py_HUGE_VAL;
+#endif
+}
+
+/* Constant nan value, generated in the same way as float('nan'). */
+/* We don't currently assume that Py_NAN is defined everywhere. */
+
+#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
+
+static double
+m_nan(void)
+{
+#ifndef PY_NO_SHORT_FLOAT_REPR
+ return _Py_dg_stdnan(0);
+#else
+ return Py_NAN;
+#endif
+}
+
+#endif
+
static double
m_tgamma(double x)
{
@@ -2009,7 +2038,11 @@ PyInit_math(void)
PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
+ PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
+#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
+ PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
+#endif
- finally:
+ finally:
return m;
}