Skip to content

Fix new numerical error in digamma and polygamma due to removal of promotion - #1677

Merged
mborland merged 4 commits into
developfrom
1668
Nov 27, 2026
Merged

mborland merged 4 commits into
developfrom
1668

Conversation

@mborland

Copy link
Copy Markdown
Member

Problem

digamma and polygamma reflect negative arguments through 1 - x, and both took the trigonometric part of the reflection from 1 - x after it had been rounded. Whenever 1 - x is inexact, the cotangent (or its derivative) is evaluated at a different point than the one asked for. Before #1554 double was evaluated in long double, where 1 - x is exact for these inputs, so this only became visible once promotion was turned off. long double itself has always been affected.

Two symptoms:

  • A pole error at non-integer x, when 1 - x rounds to an integer (for example digamma(-1 - eps) and digamma(-4503599627370495.5)).
  • Lost digits at ordinary negative x (45 ulps at x = -3.5520310993343904).

Fix

  • digamma.hpp, both overloads: the remainder for tan is taken from x - floor(x), which is exact, and the pole check is done before reflecting.
  • detail/polygamma.hpp: poly_cot_pi takes cos_pi from the argument that was not rounded, as it already did for sin_pi.

Three things go beyond the change proposed in the issue:

  • The generic (integral_constant<int, 0>) digamma overload assigned result = in its integer and half-integer special cases, which discarded the reflection term. That was unreachable while a rounded integer was reported as a pole. The special cases are now summed separately and added.
  • The cotangent term is skipped at exact half-integers, where it is exactly zero, rather than computed as pi / tan(pi / 2).
  • The pole error now reports the original x rather than 1 - (1 - x).

Results

The reproducer from the issue, x86-64 with 80-bit long double:

Call Before After Reference
digamma(-3.5520310993343904) 0.88358026830845859 0.8835802683084637 0.88358026830846363
polygamma(4, -3.5520310993343904) -863.39638087982996 -863.39638087982269 -863.39638087982276
polygamma(3, -4503599627370495.5) 584.45454620401449 194.81818206800483 194.81818206800487
digamma(-4503599627370495.5) pole error 36.043653389117154 36.043653389117156
long double polygamma(3, -(2^63 - 0.5)) 584.45454620401462 194.81818206800487 194.81818206800487

A sweep of 296 doubles of the form -(2^k - r) against a 100 digit cpp_bin_float reference:

Function Over 8 ulps before Over 8 ulps after Max after
digamma 141, plus 5 pole errors 2 32.5 ulps
polygamma, n = 2, 3, 4 139 to 142 0 7.8 ulps

trigamma was already correct and is unchanged.

Tests

test/git_issue_1668.cpp runs float, double, long double and real_concept under a policy with promotion off, so every type is evaluated at its own precision. It covers:

  • The largest half-integer of each type, where 1 - x rounds to an integer.
  • The nearest values either side of a pole.
  • A point where 1 - x rounds to a small half-integer, which exercises the half-integer branch of the generic overload.
  • The point from the issue at n = 0, 2, 3, 4 and 24 (n = 24 is past the tabulated orders).
  • The negative integers, which must still be poles.

Every numeric check fails on develop except polygamma(24, x), which guards the sign of the cosine.

Run locally: test_digamma, test_polygamma, test_trigamma and the new test with b2 on arm64 macOS, and the new test in Docker on x86-64, i386, arm32, arm64, s390x, ppc64le and ppc64 with gcc and clang. Not run locally: MSVC, and the CUDA and NVRTC digamma tests.

Remaining limits

  • Near the negative zeros of digamma the result is still off by hundreds of ulps in double (245 at x = -4.652). The two reflection terms cancel there, and promotion used to hide it on x86. Fixing it needs extra precision and is not attempted here.
  • Higher orders are less accurate without promotion: n = 7 reaches about 15 ulps, and n = 25 about 200.

Closes: #1668

Once this cycles green I will ask to cherry pick it onto the 1.93 beta since we don't want to ship this new numerical error

@mborland

Copy link
Copy Markdown
Member Author

@NAThompson suggested adding compensated arithmetic as well. These are the resulting ULPs plots for this PR vs develop:

digamma_ulps_first_zero digamma_ulps polygamma4_ulps

@mborland
mborland merged commit 8220e91 into develop Nov 27, 2026
56 checks passed
@mborland
mborland deleted the 1668 branch November 28, 2026 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

digamma and polygamma reflect through the rounded 1 - x: since #1554, double gives a pole error at non-integer x and loses digits at ordinary negative x

1 participant