From d5bd0a011930f2bdbd0c9cb89121cb27ec3f0226 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 17 Jun 2025 08:41:49 +0300 Subject: [PATCH] gh-133895: correct values of cmath.cosh/sinh in case of overflows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a split-off gh-134995. The C17 standard says (cis(y) is defined as cos(y) + i sin(y)): * ccosh(+∞ + i0) returns +∞ + i0. * ccosh(+∞ + iy) returns +∞ cis(y), for finite nonzero y. and * csinh(+∞ + i0) returns +∞ + i0. * csinh(+∞ + iy) returns +∞ cis(y), for positive finite y. So far values, computed for exceptions, aren't accessible from the pure-Python world, yet we are trying to be correct in other places. The Lib/test/mathdata/cmath_testcases.txt has data points with correct numbers (see cosh0032 and sinh0032). Also, use AC magic for the rect() value. --- Modules/clinic/cmathmodule.c.h | 18 +++++++++++++++--- Modules/cmathmodule.c | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h index 7f9e65baf120ea..774505f2fe0954 100644 --- a/Modules/clinic/cmathmodule.c.h +++ b/Modules/clinic/cmathmodule.c.h @@ -744,7 +744,7 @@ PyDoc_STRVAR(cmath_rect__doc__, #define CMATH_RECT_METHODDEF \ {"rect", _PyCFunction_CAST(cmath_rect), METH_FASTCALL, cmath_rect__doc__}, -static PyObject * +static Py_complex cmath_rect_impl(PyObject *module, double r, double phi); static PyObject * @@ -753,6 +753,7 @@ cmath_rect(PyObject *module, PyObject *const *args, Py_ssize_t nargs) PyObject *return_value = NULL; double r; double phi; + Py_complex _return_value; if (!_PyArg_CheckPositional("rect", nargs, 2, 2)) { goto exit; @@ -777,7 +778,18 @@ cmath_rect(PyObject *module, PyObject *const *args, Py_ssize_t nargs) goto exit; } } - return_value = cmath_rect_impl(module, r, phi); + _return_value = cmath_rect_impl(module, r, phi); + if (errno == EDOM) { + PyErr_SetString(PyExc_ValueError, "math domain error"); + goto exit; + } + else if (errno == ERANGE) { + PyErr_SetString(PyExc_OverflowError, "math range error"); + goto exit; + } + else { + return_value = PyComplex_FromCComplex(_return_value); + } exit: return return_value; @@ -985,4 +997,4 @@ cmath_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec exit: return return_value; } -/*[clinic end generated code: output=631db17fb1c79d66 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=7b68f197b24bcb78 input=a9049054013a1b77]*/ diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index 81cbf0d554de3c..c49ae7dd060f84 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -438,6 +438,9 @@ cmath_cosh_impl(PyObject *module, Py_complex z) x_minus_one = z.real - copysign(1., z.real); r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E; r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E; + if (isnan(r.imag)) { + r.imag = copysign(0., z.real*z.imag); + } } else { r.real = cos(z.imag) * cosh(z.real); r.imag = sin(z.imag) * sinh(z.real); @@ -674,6 +677,9 @@ cmath_sinh_impl(PyObject *module, Py_complex z) x_minus_one = z.real - copysign(1., z.real); r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E; r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E; + if (isnan(r.imag)) { + r.imag = copysign(0., z.imag); + } } else { r.real = cos(z.imag) * sinh(z.real); r.imag = sin(z.imag) * cosh(z.real); @@ -972,7 +978,7 @@ cmath_polar_impl(PyObject *module, Py_complex z) static Py_complex rect_special_values[7][7]; /*[clinic input] -cmath.rect +cmath.rect -> Py_complex_protected r: double phi: double @@ -981,9 +987,9 @@ cmath.rect Convert from polar coordinates to rectangular coordinates. [clinic start generated code]*/ -static PyObject * +static Py_complex cmath_rect_impl(PyObject *module, double r, double phi) -/*[clinic end generated code: output=385a0690925df2d5 input=24c5646d147efd69]*/ +/*[clinic end generated code: output=74ff3d17585f3388 input=50e60c5d28c834e6]*/ { Py_complex z; errno = 0; @@ -1027,11 +1033,7 @@ cmath_rect_impl(PyObject *module, double r, double phi) z.imag = r * sin(phi); errno = 0; } - - if (errno != 0) - return math_error(); - else - return PyComplex_FromCComplex(z); + return z; } /*[clinic input]