From: Serhiy Storchaka Date: Mon, 20 Jul 2026 05:56:50 +0000 (+0300) Subject: gh-154146: Fix accuracy of the math.acospi() fallback near 1 (GH-154148) X-Git-Url: http://git.ipfire.org/index.cgi?a=commitdiff_plain;h=69f080144972d1dd0acd445d776cdaea788923ed;p=thirdparty%2FPython%2Fcpython.git gh-154146: Fix accuracy of the math.acospi() fallback near 1 (GH-154148) Co-authored-by: Claude Fable 5 --- diff --git a/Misc/NEWS.d/next/Library/2026-07-19-19-05-00.gh-issue-154146.acospi.rst b/Misc/NEWS.d/next/Library/2026-07-19-19-05-00.gh-issue-154146.acospi.rst new file mode 100644 index 000000000000..5dbddbadee0b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-19-19-05-00.gh-issue-154146.acospi.rst @@ -0,0 +1,2 @@ +Fix accuracy of :func:`math.acospi` for arguments close to 1 +on platforms that do not provide ``acospi()`` in libm. diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 64e5372d73d2..eaa1850b8aee 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -220,6 +220,12 @@ static const double logpi = 1.144729885849400174143427351353058711647; static double m_acospi(double x) { + if (x >= 0.5) { + /* acos(x) = 2*asin(sqrt((1 - x)/2)). 1 - x is exact here. + Some libms (old fdlibm derivatives) lose precision in acos(x) + near x = 1, while asin() is accurate for small arguments. */ + return 2.0*asin(sqrt((1.0 - x)/2.0))/pi; + } double r = acos(x)/pi; if (isgreater(r, 1.0)) { return 1.0;