Skip to content

Commit a5d0c7c

Browse files
committed
Issue #23185: add math.inf and math.nan constants.
1 parent 845b14c commit a5d0c7c

5 files changed

Lines changed: 69 additions & 1 deletion

File tree

Doc/library/math.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ Constants
383383
The mathematical constant e = 2.718281..., to available precision.
384384

385385

386+
.. data:: inf
387+
388+
A floating-point positive infinity. (For negative infinity, use
389+
``-math.inf``.) Equivalent to the output of ``float('inf')``.
390+
391+
.. versionadded:: 3.5
392+
393+
394+
.. data:: nan
395+
396+
A floating-point "not a number" (NaN) value. Equivalent to the output of
397+
``float('nan')``.
398+
399+
.. versionadded:: 3.5
400+
401+
386402
.. impl-detail::
387403

388404
The :mod:`math` module consists mostly of thin wrappers around the platform C

Doc/whatsnew/3.5.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ re
243243
* Now unmatched groups are replaced with empty strings in :func:`re.sub`
244244
and :func:`re.subn`. (Contributed by Serhiy Storchaka in :issue:`1519638`.)
245245

246+
math
247+
----
248+
249+
* :data:`math.inf` and :data:`math.nan` constants added. (Contributed by Mark
250+
Dickinson in :issue:`23185`.)
251+
246252
shutil
247253
------
248254

Lib/test/test_math.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,17 @@ def testIsinf(self):
983983
self.assertFalse(math.isinf(0.))
984984
self.assertFalse(math.isinf(1.))
985985

986+
@requires_IEEE_754
987+
def test_nan_constant(self):
988+
self.assertTrue(math.isnan(math.nan))
989+
990+
@requires_IEEE_754
991+
def test_inf_constant(self):
992+
self.assertTrue(math.isinf(math.inf))
993+
self.assertGreater(math.inf, 0.0)
994+
self.assertEqual(math.inf, float("inf"))
995+
self.assertEqual(-math.inf, float("-inf"))
996+
986997
# RED_FLAG 16-Oct-2000 Tim
987998
# While 2.0 is more consistent about exceptions than previous releases, it
988999
# still fails this part of the test on some platforms. For now, we only

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ Core and Builtins
203203
Library
204204
-------
205205

206+
- Issue #23185: Add math.inf and math.nan constants.
207+
206208
- Issue #23186: Add ssl.SSLObject.shared_ciphers() and
207209
ssl.SSLSocket.shared_ciphers() to fetch the client's list ciphers sent at
208210
handshake.

Modules/mathmodule.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,35 @@ lanczos_sum(double x)
223223
return num/den;
224224
}
225225

226+
/* Constant for +infinity, generated in the same way as float('inf'). */
227+
228+
static double
229+
m_inf(void)
230+
{
231+
#ifndef PY_NO_SHORT_FLOAT_REPR
232+
return _Py_dg_infinity(0);
233+
#else
234+
return Py_HUGE_VAL;
235+
#endif
236+
}
237+
238+
/* Constant nan value, generated in the same way as float('nan'). */
239+
/* We don't currently assume that Py_NAN is defined everywhere. */
240+
241+
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
242+
243+
static double
244+
m_nan(void)
245+
{
246+
#ifndef PY_NO_SHORT_FLOAT_REPR
247+
return _Py_dg_stdnan(0);
248+
#else
249+
return Py_NAN;
250+
#endif
251+
}
252+
253+
#endif
254+
226255
static double
227256
m_tgamma(double x)
228257
{
@@ -2009,7 +2038,11 @@ PyInit_math(void)
20092038

20102039
PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
20112040
PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
2041+
PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
2042+
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
2043+
PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
2044+
#endif
20122045

2013-
finally:
2046+
finally:
20142047
return m;
20152048
}

0 commit comments

Comments
 (0)