File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+
246252shutil
247253------
248254
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -203,6 +203,8 @@ Core and Builtins
203203Library
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.
Original file line number Diff line number Diff 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+
226255static double
227256m_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}
You can’t perform that action at this time.
0 commit comments