diff -r d2fbefb1c818 Doc/library/math.rst --- a/Doc/library/math.rst Wed Jan 07 11:42:38 2015 -0600 +++ b/Doc/library/math.rst Wed Jan 07 19:26:20 2015 +0000 @@ -383,6 +383,21 @@ The mathematical constant e = 2.718281..., to available precision. +.. data:: inf + + A floating-point representation of positive infinity. (For negative + infinity, use ``-math.inf``). + + .. versionadded:: 3.5 + + +.. data:: nan + + A floating-point NaN. 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 -r d2fbefb1c818 Doc/whatsnew/3.5.rst --- a/Doc/whatsnew/3.5.rst Wed Jan 07 11:42:38 2015 -0600 +++ b/Doc/whatsnew/3.5.rst Wed Jan 07 19:26:20 2015 +0000 @@ -243,6 +243,12 @@ * 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 -r d2fbefb1c818 Lib/test/test_math.py --- a/Lib/test/test_math.py Wed Jan 07 11:42:38 2015 -0600 +++ b/Lib/test/test_math.py Wed Jan 07 19:26:20 2015 +0000 @@ -983,6 +983,16 @@ 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")) + # 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 -r d2fbefb1c818 Misc/NEWS --- a/Misc/NEWS Wed Jan 07 11:42:38 2015 -0600 +++ b/Misc/NEWS Wed Jan 07 19:26:20 2015 +0000 @@ -199,6 +199,8 @@ 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 -r d2fbefb1c818 Modules/mathmodule.c --- a/Modules/mathmodule.c Wed Jan 07 11:42:38 2015 -0600 +++ b/Modules/mathmodule.c Wed Jan 07 19:26:20 2015 +0000 @@ -223,6 +223,35 @@ return num/den; } +/* Constant for +infinity, generated in the same way as float('inf'). */ + +static double +m_inf() +{ +#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() +{ +#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 @@ 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; }