Feature or enhancement
Proposal:
C23 standardized multiple new trig functions in <math.h> that operate on units of “half turns”. These functions are recommended by IEEE754-2019. I propose adding these functions to CPython.
These function can be both more accurate and more performant than the traditional functions which take radian arguments, because inexact range reduction (e.g., modulo 2π) can be replaced by exact calculations (e.g., fmod(x, 2)) that never require excess precision.
Modern Linux systems such as Debian Trixie using glibc 2.41 include an implementation of all these C23 functions (regardless of whether building in C23 mode).
For instance, consider calculating the sine of 500π or 500 half-turns. The result is exactly zero with a proper math.sinpi implementation but nonzero with the traditional math.sin function, because 500π is not an exact FP number:
>>> math.sinpi(500), math.sin(math.pi*500)
(0.0, -1.6070832296378168e-13)
For platforms which do not directly support these functions, for instance because they do not support the C23 standard or do not provide the functions when not building in C23 mode, implementations similar to the existing internal function mathmodule.c function m_sinpi can be used. These perform range reduction with fmod & special value checking before delegating to a platform radians-based routine (e.g., m_sinpi → sin() or cos()) with necessary scaling by a factor of π.
I have prototyped this addition. You can view it on github: https://github.com/python/cpython/compare/main...jepler:cpython:c23-math-pifuns?expand=1#diff
This branch includes new tests & documentation for the new functions.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/add-half-turns-functions-cospi-et-al-to-math-module/107550 [no replies yet]
Feature or enhancement
Proposal:
C23 standardized multiple new trig functions in
<math.h>that operate on units of “half turns”. These functions are recommended by IEEE754-2019. I propose adding these functions to CPython.These function can be both more accurate and more performant than the traditional functions which take radian arguments, because inexact range reduction (e.g., modulo 2π) can be replaced by exact calculations (e.g.,
fmod(x, 2)) that never require excess precision.Modern Linux systems such as Debian Trixie using glibc 2.41 include an implementation of all these C23 functions (regardless of whether building in C23 mode).
For instance, consider calculating the sine of 500π or 500 half-turns. The result is exactly zero with a proper
math.sinpiimplementation but nonzero with the traditionalmath.sinfunction, because 500π is not an exact FP number:For platforms which do not directly support these functions, for instance because they do not support the C23 standard or do not provide the functions when not building in C23 mode, implementations similar to the existing internal function
mathmodule.cfunctionm_sinpican be used. These perform range reduction withfmod& special value checking before delegating to a platform radians-based routine (e.g., m_sinpi →sin()orcos()) with necessary scaling by a factor of π.I have prototyped this addition. You can view it on github: https://github.com/python/cpython/compare/main...jepler:cpython:c23-math-pifuns?expand=1#diff
This branch includes new tests & documentation for the new functions.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/add-half-turns-functions-cospi-et-al-to-math-module/107550 [no replies yet]