Skip to content

Commit 45d64ef

Browse files
hpkfftskirpichev
andauthored
gh-153144: Avoid checking errno for math.atan2() (#153148)
math.atan2() and math.atan2pi() no longer raise ValueError. cmath.phase() no longer raises OverflowError or ValueError, and the values of math.atan2(b, a) and cmath.phase(complex(a, b)) always match. The following functions no longer set errno to zero: cmath.phase(), math.atan2(), math.atan2pi(), and math.copysign(). Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
1 parent 9fd3a6f commit 45d64ef

6 files changed

Lines changed: 71 additions & 15 deletions

File tree

‎Lib/test/test_cmath.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,6 @@ def polar_with_errno_set(z):
406406
_testcapi.set_errno(0)
407407
self.check_polar(polar_with_errno_set)
408408

409-
@unittest.skipIf(sys.platform.startswith("sunos"),
410-
"skipping, see gh-138573")
411409
def test_phase(self):
412410
self.assertAlmostEqual(phase(0), 0.)
413411
self.assertAlmostEqual(phase(1.), 0.)
@@ -423,6 +421,16 @@ def test_phase(self):
423421
self.assertEqual(phase(complex(-0.0, 0.0)), pi)
424422
self.assertEqual(phase(complex(-0.0, -0.0)), -pi)
425423

424+
# overflow and underflow of imag/real
425+
self.assertEqual(phase(complex(1E300, 1E-320)), 0.0)
426+
self.assertEqual(phase(complex(1E300, -1E-320)), -0.0)
427+
self.assertAlmostEqual(phase(complex(1E-320, 1E300)), pi/2)
428+
self.assertAlmostEqual(phase(complex(-1E-320, 1E300)), pi/2)
429+
self.assertAlmostEqual(phase(complex(-1E300, 1E-320)), pi)
430+
self.assertAlmostEqual(phase(complex(-1E300, -1E-320)), -pi)
431+
self.assertAlmostEqual(phase(complex(1E-320, -1E300)), -pi/2)
432+
self.assertAlmostEqual(phase(complex(-1E-320, -1E300)), -pi/2)
433+
426434
# infinities
427435
self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi)
428436
self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi)

‎Lib/test/test_math.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,6 @@ def testAtanh(self):
287287
self.assertRaises(ValueError, math.atanh, NINF)
288288
self.assertTrue(math.isnan(math.atanh(NAN)))
289289

290-
@unittest.skipIf(sys.platform.startswith("sunos"),
291-
"skipping, see gh-138573")
292290
def testAtan2(self):
293291
self.assertRaises(TypeError, math.atan2)
294292
self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
@@ -350,6 +348,15 @@ def testAtan2(self):
350348
self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
351349
self.assertTrue(math.isnan(math.atan2(NAN, INF)))
352350
self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
351+
# overflow and underflow of y/x
352+
self.assertEqual(math.atan2(1E-320, 1E300), 0.0)
353+
self.assertEqual(math.atan2(-1E-320, 1E300), -0.0)
354+
self.assertAlmostEqual(math.atan2(1E300, 1E-320), math.pi/2)
355+
self.assertAlmostEqual(math.atan2(1E300, -1E-320), math.pi/2)
356+
self.assertAlmostEqual(math.atan2(1E-320, -1E300), math.pi)
357+
self.assertAlmostEqual(math.atan2(-1E-320, -1E300), -math.pi)
358+
self.assertAlmostEqual(math.atan2(-1E300, 1E-320), -math.pi/2)
359+
self.assertAlmostEqual(math.atan2(-1E300, -1E-320), -math.pi/2)
353360

354361
def testCbrt(self):
355362
self.assertRaises(TypeError, math.cbrt)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:func:`math.atan2` and :func:`math.atan2pi` no longer raise :exc:`ValueError`.
2+
:func:`cmath.phase` no longer raises :exc:`OverflowError` or :exc:`ValueError`,
3+
and the values of ``math.atan2(b, a)`` and ``cmath.phase(complex(a, b))`` always
4+
match. Contributed by High Performance Kernels LLC.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The following functions no longer set ``errno`` to zero: :func:`cmath.phase`,
2+
:func:`math.atan2`, :func:`math.atan2pi`, and :func:`math.copysign`.
3+
Contributed by High Performance Kernels LLC.

‎Modules/cmathmodule.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,11 @@ cmath_phase_impl(PyObject *module, Py_complex z)
10041004
{
10051005
double phi;
10061006

1007-
errno = 0;
1008-
phi = atan2(z.imag, z.real); /* should not cause any exception */
1009-
if (errno != 0)
1010-
return math_error();
1011-
else
1012-
return PyFloat_FromDouble(phi);
1007+
phi = atan2(z.imag, z.real);
1008+
/* gh-153144: Ignore atan2() errno on purpose since it can optionally be
1009+
EDOM, which we should ignore, or ERANGE if phi underflows,
1010+
which is silent on Python. Overflow is not possible. */
1011+
return PyFloat_FromDouble(phi);
10131012
}
10141013

10151014
/*[clinic input]

‎Modules/mathmodule.c‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ math_1a(PyObject *arg, double (*func) (double), const char *err_msg)
10171017
The last rule is used to catch overflow on platforms which follow
10181018
C89 but for which HUGE_VAL is not an infinity.
10191019
1020-
For most two-argument functions (copysign, fmod, hypot, atan2)
1020+
For most two-argument functions (fmod, hypot)
10211021
these rules are enough to ensure that Python's functions behave as
10221022
specified in 'Annex F' of the C99 standard, with the 'invalid' and
10231023
'divide-by-zero' floating-point exceptions mapping to Python's
@@ -1030,8 +1030,9 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10301030
double (*func) (double, double), const char *funcname)
10311031
{
10321032
double x, y, r;
1033-
if (!_PyArg_CheckPositional(funcname, nargs, 2, 2))
1033+
if (!_PyArg_CheckPositional(funcname, nargs, 2, 2)) {
10341034
return NULL;
1035+
}
10351036
x = PyFloat_AsDouble(args[0]);
10361037
if (x == -1.0 && PyErr_Occurred()) {
10371038
return NULL;
@@ -1060,6 +1061,29 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10601061
return PyFloat_FromDouble(r);
10611062
}
10621063

1064+
/* variant of math_2, to be used when the function being wrapped is known NOT
1065+
to need error checking (i.e., no overflow, invalid, or divide-by-zero). */
1066+
1067+
static PyObject *
1068+
math_2ne(PyObject *const *args, Py_ssize_t nargs,
1069+
double (*func) (double, double), const char *funcname)
1070+
{
1071+
double x, y, r;
1072+
if (!_PyArg_CheckPositional(funcname, nargs, 2, 2)) {
1073+
return NULL;
1074+
}
1075+
x = PyFloat_AsDouble(args[0]);
1076+
if (x == -1.0 && PyErr_Occurred()) {
1077+
return NULL;
1078+
}
1079+
y = PyFloat_AsDouble(args[1]);
1080+
if (y == -1.0 && PyErr_Occurred()) {
1081+
return NULL;
1082+
}
1083+
r = (*func)(x, y); // Ignore errno on purpose.
1084+
return PyFloat_FromDouble(r);
1085+
}
1086+
10631087
#define FUNC1(funcname, func, can_overflow, docstring) \
10641088
static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
10651089
return math_1(args, func, can_overflow, NULL); \
@@ -1090,6 +1114,12 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10901114
}\
10911115
PyDoc_STRVAR(math_##funcname##_doc, docstring);
10921116

1117+
#define FUNC2NE(funcname, func, docstring) \
1118+
static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \
1119+
return math_2ne(args, nargs, func, #funcname); \
1120+
}\
1121+
PyDoc_STRVAR(math_##funcname##_doc, docstring);
1122+
10931123
FUNC1D(acos, acos, 0,
10941124
"acos($module, x, /)\n--\n\n"
10951125
"Return the arc cosine (measured in radians) of x.\n\n"
@@ -1121,11 +1151,14 @@ FUNC1(atan, atan, 0,
11211151
"atan($module, x, /)\n--\n\n"
11221152
"Return the arc tangent (measured in radians) of x.\n\n"
11231153
"The result is between -pi/2 and pi/2.")
1124-
FUNC2(atan2, atan2,
1154+
/* gh-153144: Ignore atan2 and atan2pi errno on purpose since it can optionally
1155+
* be EDOM, which we should ignore, or ERANGE on underflow, which is
1156+
* silent on Python. Overflow is not possible. */
1157+
FUNC2NE(atan2, atan2,
11251158
"atan2($module, y, x, /)\n--\n\n"
11261159
"Return the arc tangent (measured in radians) of y/x.\n\n"
11271160
"Unlike atan(y/x), the signs of both x and y are considered.")
1128-
FUNC2(atan2pi, m_atan2pi,
1161+
FUNC2NE(atan2pi, m_atan2pi,
11291162
"atan2pi($module, y, x, /)\n--\n\n"
11301163
"Return the arc tangent (measured in half-turns) of y/x.\n\n"
11311164
"Unlike atanpi(y/x), the signs of both x and y are considered.")
@@ -1178,7 +1211,9 @@ math_ceil(PyObject *module, PyObject *number)
11781211
return PyLong_FromDouble(ceil(x));
11791212
}
11801213

1181-
FUNC2(copysign, copysign,
1214+
/* gh-153144: Ignore copysign errno on purpose since IEEE Std 754-2019 requires
1215+
* it to operate on bit strings and to signal no exception. */
1216+
FUNC2NE(copysign, copysign,
11821217
"copysign($module, x, y, /)\n--\n\n"
11831218
"Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
11841219
"On platforms that support signed zeros, copysign(1.0, -0.0)\n"

0 commit comments

Comments
 (0)