Skip to content

Commit 7a6ea3e

Browse files
committed
Issue 23229: add cmath.inf, cmath.nan, cmath.infj and cmath.nanj.
1 parent 39944c7 commit 7a6ea3e

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

Doc/library/cmath.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,34 @@ Constants
259259

260260
.. versionadded:: 3.6
261261

262+
.. data:: inf
263+
264+
Floating-point positive infinity. Equivalent to ``float('inf')``.
265+
266+
.. versionadded:: 3.6
267+
268+
.. data:: infj
269+
270+
Complex number with zero real part and positive infinity imaginary
271+
part. Equivalent to ``complex(0.0, float('inf'))``.
272+
273+
.. versionadded:: 3.6
274+
275+
.. data:: nan
276+
277+
A floating-point "not a number" (NaN) value. Equivalent to
278+
``float('nan')``.
279+
280+
.. versionadded:: 3.6
281+
282+
.. data:: nanj
283+
284+
Complex number with zero real part and NaN imaginary part. Equivalent to
285+
``complex(0.0, float('nan'))``.
286+
287+
.. versionadded:: 3.6
288+
289+
262290
.. index:: module: math
263291

264292
Note that the selection of functions is similar, but not identical, to that in

Lib/test/test_cmath.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ def test_constants(self):
154154
self.assertAlmostEqual(cmath.e, e_expected, places=9,
155155
msg="cmath.e is {}; should be {}".format(cmath.e, e_expected))
156156

157+
def test_infinity_and_nan_constants(self):
158+
self.assertEqual(cmath.inf.real, math.inf)
159+
self.assertEqual(cmath.inf.imag, 0.0)
160+
self.assertEqual(cmath.infj.real, 0.0)
161+
self.assertEqual(cmath.infj.imag, math.inf)
162+
163+
self.assertTrue(math.isnan(cmath.nan.real))
164+
self.assertEqual(cmath.nan.imag, 0.0)
165+
self.assertEqual(cmath.nanj.real, 0.0)
166+
self.assertTrue(math.isnan(cmath.nanj.imag))
167+
168+
# Check consistency with reprs.
169+
self.assertEqual(repr(cmath.inf), "inf")
170+
self.assertEqual(repr(cmath.infj), "infj")
171+
self.assertEqual(repr(cmath.nan), "nan")
172+
self.assertEqual(repr(cmath.nanj), "nanj")
173+
157174
def test_user_object(self):
158175
# Test automatic calling of __complex__ and __float__ by cmath
159176
# functions

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Core and Builtins
4949
Library
5050
-------
5151

52+
- Issue #23229: Add new ``cmath`` constants: ``cmath.inf`` and ``cmath.nan`` to
53+
match ``math.inf`` and ``math.nan``, and also ``cmath.infj`` and
54+
``cmath.nanj`` to match the format used by complex repr.
55+
5256
- Issue #27861: Fixed a crash in sqlite3.Connection.cursor() when a factory
5357
creates not a cursor. Patch by Xiang Zhang.
5458

Modules/cmathmodule.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,54 @@ else {
8181
#endif
8282
#define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2)
8383

84+
/* Constants cmath.inf, cmath.infj, cmath.nan, cmath.nanj.
85+
cmath.nan and cmath.nanj are defined only when either
86+
PY_NO_SHORT_FLOAT_REPR is *not* defined (which should be
87+
the most common situation on machines using an IEEE 754
88+
representation), or Py_NAN is defined. */
89+
90+
static double
91+
m_inf(void)
92+
{
93+
#ifndef PY_NO_SHORT_FLOAT_REPR
94+
return _Py_dg_infinity(0);
95+
#else
96+
return Py_HUGE_VAL;
97+
#endif
98+
}
99+
100+
static Py_complex
101+
c_infj(void)
102+
{
103+
Py_complex r;
104+
r.real = 0.0;
105+
r.imag = m_inf();
106+
return r;
107+
}
108+
109+
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
110+
111+
static double
112+
m_nan(void)
113+
{
114+
#ifndef PY_NO_SHORT_FLOAT_REPR
115+
return _Py_dg_stdnan(0);
116+
#else
117+
return Py_NAN;
118+
#endif
119+
}
120+
121+
static Py_complex
122+
c_nanj(void)
123+
{
124+
Py_complex r;
125+
r.real = 0.0;
126+
r.imag = m_nan();
127+
return r;
128+
}
129+
130+
#endif
131+
84132
/* forward declarations */
85133
static Py_complex cmath_asinh_impl(PyObject *, Py_complex);
86134
static Py_complex cmath_atanh_impl(PyObject *, Py_complex);
@@ -1240,6 +1288,12 @@ PyInit_cmath(void)
12401288
PyFloat_FromDouble(Py_MATH_PI));
12411289
PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
12421290
PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */
1291+
PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
1292+
PyModule_AddObject(m, "infj", PyComplex_FromCComplex(c_infj()));
1293+
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
1294+
PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
1295+
PyModule_AddObject(m, "nanj", PyComplex_FromCComplex(c_nanj()));
1296+
#endif
12431297

12441298
/* initialize special value tables */
12451299

0 commit comments

Comments
 (0)