From bb40d8a09d8ef25ebf3dd00c4e69b3a451282550 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 22 Apr 2025 13:24:40 +0200 Subject: [PATCH] Update math error messages for 3.14 (2) --- mypyc/lib-rt/float_ops.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mypyc/lib-rt/float_ops.c b/mypyc/lib-rt/float_ops.c index 48ebc44431da..319065742559 100644 --- a/mypyc/lib-rt/float_ops.c +++ b/mypyc/lib-rt/float_ops.c @@ -34,6 +34,15 @@ static double CPy_MathExpectedPositiveInputError(double x) { return CPY_FLOAT_ERROR; } +static double CPy_MathExpectedFiniteInput(double x) { + char *buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL); + if (buf) { + PyErr_Format(PyExc_ValueError, "expected a finite input, got %s", buf); + PyMem_Free(buf); + } + return CPY_FLOAT_ERROR; +} + double CPyFloat_FromTagged(CPyTagged x) { if (CPyTagged_CheckShort(x)) { return CPyTagged_ShortAsSsize_t(x); @@ -48,7 +57,11 @@ double CPyFloat_FromTagged(CPyTagged x) { double CPyFloat_Sin(double x) { double v = sin(x); if (unlikely(isnan(v)) && !isnan(x)) { +#if CPY_3_14_FEATURES + return CPy_MathExpectedFiniteInput(x); +#else return CPy_DomainError(); +#endif } return v; } @@ -56,14 +69,22 @@ double CPyFloat_Sin(double x) { double CPyFloat_Cos(double x) { double v = cos(x); if (unlikely(isnan(v)) && !isnan(x)) { +#if CPY_3_14_FEATURES + return CPy_MathExpectedFiniteInput(x); +#else return CPy_DomainError(); +#endif } return v; } double CPyFloat_Tan(double x) { if (unlikely(isinf(x))) { +#if CPY_3_14_FEATURES + return CPy_MathExpectedFiniteInput(x); +#else return CPy_DomainError(); +#endif } return tan(x); }