gh-150534: Add C23 half-turn trigonometric *pi functions#150555
gh-150534: Add C23 half-turn trigonometric *pi functions#150555jepler wants to merge 21 commits into
*pi functions#150555Conversation
* Add tests * Add documentation * Add configure-time checks for function presence * Regenerate configure No news item added, pending creation of a github issue. As committed, the local implementation of the functions is always used. For production, the #undefs should be removed so that the presence of the functions is detected by configure and used by mathmodule.
Documentation build overview
11 files changed ·
|
|
|
||
| acospi10000 acospi 0.0 -> 0.5 | ||
| acospi10001 acospi -0.0 -> 0.5 | ||
| acospi10002 acospi 1.0 -> 0.0 |
There was a problem hiding this comment.
Please also test infinite/nan input and values with magnitude > 1. Include appropriate FPE flags, where it's required by the C23 Annex F.
With this, I think you can drop separate tests in the test_math.py for new functions.
| The vector in the plane from the origin to point ``(x, y)`` makes this angle | ||
| with the positive X axis. The point of :func:`atan2pi` is that the signs of both | ||
| inputs are known to it, so it can compute the correct quadrant for the angle. | ||
| For example, ``atanpi(1)`` and ``atanpi2(1, 1)`` are both ``1/4``, but | ||
| ``atanpi2(-1, -1)`` is ``-3/4``. |
There was a problem hiding this comment.
| The vector in the plane from the origin to point ``(x, y)`` makes this angle | |
| with the positive X axis. The point of :func:`atan2pi` is that the signs of both | |
| inputs are known to it, so it can compute the correct quadrant for the angle. | |
| For example, ``atanpi(1)`` and ``atanpi2(1, 1)`` are both ``1/4``, but | |
| ``atanpi2(-1, -1)`` is ``-3/4``. |
There was a problem hiding this comment.
thank you, committed locally and will push shortly. (atanpi2 -> atan2pi)
| #undef HAVE_ACOSPI | ||
| #undef HAVE_ASINPI | ||
| #undef HAVE_ATANPI | ||
| #undef HAVE_ATAN2PI | ||
| #undef HAVE_COSPI | ||
| #undef HAVE_SINPI | ||
| #undef HAVE_TANPI |
There was a problem hiding this comment.
Why you did this? libm functions will be not used, isn't?
There was a problem hiding this comment.
mistake, deleted. I add/remove this block for local testing to make sure that my implementations of the new functions are being tested.
| integral or close to an integer). It conforms to IEEE 754-2008. | ||
| Special cases are handled as for glibc 2.41 __acospi. |
There was a problem hiding this comment.
Are you sure? I don't see this function in the IEEE 754-2008. I think it come with the IEEE 754-2019 (Table 9.1).
I think it's better to reference the Annex F of C23 standard. Same below.
There was a problem hiding this comment.
I finally noticed that I copypasted "IEEE 754-2008" from an old internal comment on m_sinpi without checking. It'll be good to have this corrected.
| return cos(x); | ||
| } | ||
| x = fabs(x - 2.0 * round(0.5 * x)); | ||
| if (islessequal(x, 0.25)) { |
There was a problem hiding this comment.
As argument is finite - it doesn't make sense to use "silent" comparison functions:
| if (islessequal(x, 0.25)) { | |
| if (x <= 0.25) { |
| if (x == 0.5) { | ||
| return 0.0; | ||
| } | ||
| if (islessequal(x, 0.75)) { |
There was a problem hiding this comment.
| if (islessequal(x, 0.75)) { | |
| if (x <= 0.75) { |
| {"asinh", math_asinh, METH_O, math_asinh_doc}, | ||
| {"asinpi", math_asinpi, METH_O, math_asinpi_doc}, | ||
| {"atan", math_atan, METH_O, math_atan_doc}, | ||
| {"atan2", _PyCFunction_CAST(math_atan2), METH_FASTCALL, math_atan2_doc}, |
| acos(x)/pi, giving accurate results for all finite x (especially x | ||
| integral or close to an integer). It conforms to IEEE 754-2008. |
There was a problem hiding this comment.
This is misleading. Apparently, it's copy-pasted stub from sinpi/cospi, etc. Please correct this. Same below for other arc-functions.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Please add also a What's New entry.
|
|
||
| .. function:: atanpi(x) | ||
|
|
||
| Return the arc tangent of *x*, in half-turns. The result is between ``-1/2`` and |
There was a problem hiding this comment.
Since the result is a float, should not we use 0.5 instead 0f 1/2? The same question for other documentation and examples.
There was a problem hiding this comment.
That's a great question. These doc blocks were made by copypaste of the existing block (e.g., atan) then removing pi.
It's clear why an expression using pi is used in the existing atan2 docs. However, it's possible to ask which would be clearer in the atan2 comment: -.75*pi or -3*pi/4. I feel like the answer to this might make it clear whether -.75 or -3/4 is clearer in the new function's documentation.
Thoughts?
There was a problem hiding this comment.
-3*pi/4 is an expression. I think it is clearer in such way. But for atan2pi we can use exact number: -0.75.
I don't have a firm opinion, I'm just pointing out a possible option. I am fine with any option.
.. as suggested in review
|
Branch updated based on review comments. Thank you for your reviews. |
| acos(x)/pi, giving accurate results for all finite x (especially x | ||
| integral or close to an integer). It conforms to C23 Annex 'F'. |
There was a problem hiding this comment.
This still is misleading, just as for other arc-functions.
| /* | ||
| acos(x)/pi, giving accurate results for all finite x (especially x | ||
| integral or close to an integer). It conforms to C23 Annex 'F'. | ||
| Special cases are handled as for glibc 2.41 __acospi. |
There was a problem hiding this comment.
This is redundant, special cases described by Annex:
| Special cases are handled as for glibc 2.41 __acospi. |
Same for all comments.
Also use the surrounding style of two spaces after a sentence- ending period.
|
I've updated the PR with additional comment changes as requested. |
| For example, ``atanpi(1)`` and ``atan2pi(1, 1)`` are both ``.25``, but | ||
| ``atan2pi(-1, -1)`` is ``-.75``. |
There was a problem hiding this comment.
| For example, ``atanpi(1)`` and ``atan2pi(1, 1)`` are both ``.25``, but | |
| ``atan2pi(-1, -1)`` is ``-.75``. | |
| For example, ``atanpi(1)`` and ``atan2pi(1, 1)`` are both ``0.25``, but | |
| ``atan2pi(-1, -1)`` is ``-0.75``. |
Let's be consistent with fractional values.
| {"atan2", _PyCFunction_CAST(math_atan2), METH_FASTCALL, math_atan2_doc}, | ||
| {"atanh", math_atanh, METH_O, math_atanh_doc}, | ||
| {"atan2pi", _PyCFunction_CAST(math_atan2pi), METH_FASTCALL, math_atan2pi_doc}, | ||
| {"atanpi", math_atanpi, METH_O, math_atanpi_doc}, |
There was a problem hiding this comment.
{"atanpi", math_atanpi, METH_O, math_atanpi_doc},make it aligned with METH_O of atanh
*pi functions
See #150534 and https://discuss.python.org/t/add-half-turns-functions-cospi-et-al-to-math-module-following-c23-ieee754-2019/107550/5 for discussion and context.
The following functions are added to the math module:
The following aspects of a new feature are covered in this pull request:
The
math_testcases.txtcases are based on glibc's test cases, and their input & output values have been used. I supplemented this with tests of inf/nan arguments.I extended
math_testcases.txtso that it can test n-arg functions, and used it to test atan2pi.Closes: #150534