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
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 55/100
Hướng nghiên cứu
The bug is in the reflection step of digamma_imp in include/boost/math/special_functions/digamma.hpp (both overloads) and in poly_cot_pi in include/boost/math/special_functions/detail/polygamma.hpp. Start by compiling the reproduction in the issue against develop with -DBOOST_MATH_STANDALONE and confirming the pole error at x = -4503599627370495.5. Done means the remainder and cot terms are computed from the exact x, and the printed values match the mpmath figures given in the issue, without regressing the existing digamma and polygamma tests.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Hi! I helped develop FloatLib, a floating-point library in Lean, and I've been cross-checking special functions against mpmath and Boost for TorchLean. On develop, digamma and polygamma at negative x lost accuracy after #1554 stopped promoting double: 45 ulps at x = -3.55, and a pole error at x = -4503599627370495.5, which isn't an integer. 1.92.0 gets both right, but long double has the same problem there too. I used a bit of AI to help with the analysis below and went through it myself :)
AI-generated analysis and reproduction
#include <boost/math/special_functions/digamma.hpp>
#include <boost/math/special_functions/polygamma.hpp>
#include <cmath>
#include <cstdio>
#include <stdexcept>
int main() {
double x = -3.5520310993343904;
std::printf("digamma(%.17g) = %.17g\n", x, boost::math::digamma(x));
std::printf("polygamma(4, %.17g) = %.17g\n", x, boost::math::polygamma(4, x));
x = -4503599627370495.5; // 1 - x rounds to 2^52
std::printf("polygamma(3, %.17g) = %.17g\n", x, boost::math::polygamma(3, x));
try {
std::printf("digamma(%.17g) = %.17g\n", x, boost::math::digamma(x));
} catch (const std::exception& e) {
std::printf("digamma(%.17g): %s\n", x, e.what());
}
long double y = -(std::ldexp(1.0L, 63) - 0.5L); // long double: 1 - y rounds to 2^63
std::printf("polygamma(3, %.21Lg) = %.17Lg\n", y, boost::math::polygamma(3, y));
}
develop (9ecd4f99), g++ -O2 -std=c++17 -DBOOST_MATH_STANDALONE:
digamma(-3.5520310993343904) = 0.88358026830845859
polygamma(4, -3.5520310993343904) = -863.39638087982996
polygamma(3, -4503599627370495.5) = 584.45454620401449
digamma(-4503599627370495.5): Error in function boost::math::digamma<double>(double): Evaluation of function at pole -4503599627370495
polygamma(3, -9223372036854775807.5) = 584.45454620401462
| line | develop | 1.92.0 | mpmath (400 bits) |
|---|---|---|---|
digamma(-3.5520310993343904) |
45 ulps off | 0.88358026830846359 | 0.88358026830846363 |
polygamma(4, -3.5520310993343904) |
63 ulps off | -863.3963808798228 | -863.39638087982276 |
polygamma(3, -4503599627370495.5) |
584.45… | 194.81818206800489 | 194.81818206800487 |
digamma(-4503599627370495.5) |
pole error | 36.043653389117154 | 36.043653389117156 |
long double polygamma(3, -(2^63 - 0.5)) |
584.45… | 584.45… | 194.81818206800487 |
Looks like both reflections work from z = 1 - x after it's rounded. digamma_imp takes the remainder for tan from z (digamma.hpp L437-L444, and again at L511-L518). poly_cot_pi takes sin_pi from whichever of z and x is smaller, but cos_pi always from z (polygamma.hpp L316-L317, called at L513-L514). When 1 - x isn't exact, cos_pi(z) and the remainder are taken at a different point. At -4503599627370495.5, z is the integer 2^52, so cos_pi(z) = 1 where -cos_pi(x) = 0, and digamma sees remainder 0. Before #1554, double was evaluated in long double, where 1 - x is exact for these inputs. Long double itself has always been affected, as the last line shows (1.92.0 too).
trigamma already takes its sin_pi from x itself, so would it be OK to do every reflection's argument reduction from the exact x in the same way, and never from the rounded 1 - x? x - floor(x) is exact, and cos(π(1 - x)) = -cos(πx):
- x = 1 - x;
- T remainder = x - floor(x);
- if(remainder > T(0.5))
- {
- remainder -= 1;
- }
+ T f = x - floor(x);
+ T remainder = f < T(0.5) ? T(-f) : T(1 - f);
+ x = 1 - x;
(in both digamma_imp overloads), and in poly_cot_pi:
- T c = BOOST_MATH_NAMESPACE::cos_pi(x, pol);
+ T c = fabs(x) < fabs(xc) ? BOOST_MATH_NAMESPACE::cos_pi(x, pol) : T(-BOOST_MATH_NAMESPACE::cos_pi(xc, pol));
With those changes (tested in a local copy of develop), the snippet prints 0.8835802683084637, -863.39638087982269, 194.81818206800483, 36.043653389117154 and 194.81818206800487. I checked against mpmath at 294 points x = -(2^k - r) (k = 2..52, r random in (0, 1)) and 299 random x = -10^u. Develop is more than 8 ulps off at 148 and 6 of them for digamma (plus 8 pole errors), and at about 150 for polygamma n = 2..4. With the change, n = 2..4 are within 7.1 ulps except for one point at 9.3. Digamma is left with 7 points over 8 ulps (at most 101). All of them are near zeros of ψ, where the condition number is 232 or more and larger than the error. trigamma / polygamma(1, x) are fine either way.
Versions: Boost.Math develop 9ecd4f9958670666bd69db511a3908892769e369 (2026-11-21) and boost-1.92.0 (f12be767), header-only with -DBOOST_MATH_STANDALONE, GCC 11.5.0 (Red Hat 11.5.0-5), -O2 -std=c++17, x86-64 Linux, default policies.
- Ngôn ngữ chính
- C++
- Star
- 382
- Fork
- 274
- Merge trung bình
- 21 giờ 14 phút
- Pull request đã merge (30 ngày)
- 51
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của boostorg/math
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Fisher F complemented quantile fails for two upper-tail probabilitiesCó thể đã có người làm @hector2394 đã nhận 2 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 15/100
boostorg/math#1666 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Feature WishlistĐang mởenhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
boostorg/math#303 · 20 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của boostorg/math
Issue tương tự
-
agent:WSL code-review linux pri:medium refactoring
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
kind/enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 82/100
psi29a/d2decomped#691 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
CachyOS/packageinstaller#144 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
(S1 - Need confirmation)
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
CleverRaven/Cataclysm-DDA#89715 ·
Maintainer thường phản hồi trong vòng 1 ngày