Skip to content

Commit 69f0801

Browse files
gh-154146: Fix accuracy of the math.acospi() fallback near 1 (GH-154148)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 44ea950 commit 69f0801

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix accuracy of :func:`math.acospi` for arguments close to 1
2+
on platforms that do not provide ``acospi()`` in libm.

Modules/mathmodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ static const double logpi = 1.144729885849400174143427351353058711647;
220220
static double
221221
m_acospi(double x)
222222
{
223+
if (x >= 0.5) {
224+
/* acos(x) = 2*asin(sqrt((1 - x)/2)). 1 - x is exact here.
225+
Some libms (old fdlibm derivatives) lose precision in acos(x)
226+
near x = 1, while asin() is accurate for small arguments. */
227+
return 2.0*asin(sqrt((1.0 - x)/2.0))/pi;
228+
}
223229
double r = acos(x)/pi;
224230
if (isgreater(r, 1.0)) {
225231
return 1.0;

0 commit comments

Comments
 (0)