-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
gh-153144: Avoid checking errno for atan2
#153148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
503fd4a
c9c51ad
287e6ee
cbe759a
91257f2
25ec8e2
43f7398
5b6d005
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| The return values of ``atan2(0.0, 0.0)``, ``atan2(0.0, -0.0)``, etc. are | ||
| specified in Annex F of the C standard. The Intel and Solaris math libraries, | ||
| for legacy compatibility, also set ``errno`` to indicate a domain error when | ||
| both arguments are zero. Python's :func:`math.atan2`, :func:`math.atan2pi`, | ||
| and :func:`cmath.phase` should return the values specified by Annex F, not | ||
| raise ``ValueError``. This patch ignores ``errno`` for the C ``atan2()`` and | ||
| ``atan2pi()`` functions. Contributed by High Performance Kernels LLC. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1004,12 +1004,9 @@ cmath_phase_impl(PyObject *module, Py_complex z) | |
| { | ||
| double phi; | ||
|
|
||
| errno = 0; | ||
| phi = atan2(z.imag, z.real); /* should not cause any exception */ | ||
| if (errno != 0) | ||
| return math_error(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expected a test_cmath failure when this code path is removed. Is it because glibc math library doesn't errno in this case?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. With the exception of the Intel and Solaris math libraries, errno is not set in this case by any math library that Python cares about. I say this because 🌱 Some math libraries (e.g., musl) don't set errno for anything, so Python cannot rely on errno for detecting overflow or invalid. I would think that errno checking can be removed everywhere.... |
||
| else | ||
| return PyFloat_FromDouble(phi); | ||
| /* gh-153144: Ignore atan2() errno on purpose. */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, it's rather redundant after inline comment above.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Victor asked for all these comments. |
||
| return PyFloat_FromDouble(phi); | ||
| } | ||
|
|
||
| /*[clinic input] | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1017,7 +1017,7 @@ math_1a(PyObject *arg, double (*func) (double), const char *err_msg) | |||||
| The last rule is used to catch overflow on platforms which follow | ||||||
| C89 but for which HUGE_VAL is not an infinity. | ||||||
|
|
||||||
| For most two-argument functions (copysign, fmod, hypot, atan2) | ||||||
| For most two-argument functions (copysign, fmod, hypot) | ||||||
| these rules are enough to ensure that Python's functions behave as | ||||||
| specified in 'Annex F' of the C99 standard, with the 'invalid' and | ||||||
| 'divide-by-zero' floating-point exceptions mapping to Python's | ||||||
|
|
@@ -1060,6 +1060,28 @@ math_2(PyObject *const *args, Py_ssize_t nargs, | |||||
| return PyFloat_FromDouble(r); | ||||||
| } | ||||||
|
|
||||||
| /* variant of math_2, to be used when the function being wrapped is known NOT | ||||||
| to need error checking (i.e., no overflow, invalid, or divide-by-zero). */ | ||||||
|
|
||||||
| static PyObject * | ||||||
| math_2ne(PyObject *const *args, Py_ssize_t nargs, | ||||||
| double (*func) (double, double), const char *funcname) | ||||||
| { | ||||||
| double x, y, r; | ||||||
| if (!_PyArg_CheckPositional(funcname, nargs, 2, 2)) | ||||||
| return NULL; | ||||||
| x = PyFloat_AsDouble(args[0]); | ||||||
| if (x == -1.0 && PyErr_Occurred()) { | ||||||
| return NULL; | ||||||
| } | ||||||
| y = PyFloat_AsDouble(args[1]); | ||||||
| if (y == -1.0 && PyErr_Occurred()) { | ||||||
| return NULL; | ||||||
| } | ||||||
| r = (*func)(x, y); // Ignore errno on purpose. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, expand the comment for function? |
||||||
| return PyFloat_FromDouble(r); | ||||||
| } | ||||||
|
|
||||||
| #define FUNC1(funcname, func, can_overflow, docstring) \ | ||||||
| static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ | ||||||
| return math_1(args, func, can_overflow, NULL); \ | ||||||
|
|
@@ -1090,6 +1112,12 @@ math_2(PyObject *const *args, Py_ssize_t nargs, | |||||
| }\ | ||||||
| PyDoc_STRVAR(math_##funcname##_doc, docstring); | ||||||
|
|
||||||
| #define FUNC2NE(funcname, func, docstring) \ | ||||||
| static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \ | ||||||
| return math_2ne(args, nargs, func, #funcname); \ | ||||||
| }\ | ||||||
| PyDoc_STRVAR(math_##funcname##_doc, docstring); | ||||||
|
|
||||||
| FUNC1D(acos, acos, 0, | ||||||
| "acos($module, x, /)\n--\n\n" | ||||||
| "Return the arc cosine (measured in radians) of x.\n\n" | ||||||
|
|
@@ -1121,11 +1149,11 @@ FUNC1(atan, atan, 0, | |||||
| "atan($module, x, /)\n--\n\n" | ||||||
| "Return the arc tangent (measured in radians) of x.\n\n" | ||||||
| "The result is between -pi/2 and pi/2.") | ||||||
| FUNC2(atan2, atan2, | ||||||
| FUNC2NE(atan2, atan2, // gh-153144: Ignore atan2() errno on purpose. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "atan2($module, y, x, /)\n--\n\n" | ||||||
| "Return the arc tangent (measured in radians) of y/x.\n\n" | ||||||
| "Unlike atan(y/x), the signs of both x and y are considered.") | ||||||
| FUNC2(atan2pi, m_atan2pi, | ||||||
| FUNC2NE(atan2pi, m_atan2pi, // gh-153144: Ignore atan2pi() errno on purpose. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "atan2pi($module, y, x, /)\n--\n\n" | ||||||
| "Return the arc tangent (measured in half-turns) of y/x.\n\n" | ||||||
| "Unlike atanpi(y/x), the signs of both x and y are considered.") | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add comment:
// gh-153144: ignore atan2() errno on purpose.