diff --git a/unsupported/Eigen/src/SpecialFunctions/SpecialFunctionsImpl.h b/unsupported/Eigen/src/SpecialFunctions/SpecialFunctionsImpl.h index 5553bbccf..8e88b9dd9 100644 --- a/unsupported/Eigen/src/SpecialFunctions/SpecialFunctionsImpl.h +++ b/unsupported/Eigen/src/SpecialFunctions/SpecialFunctionsImpl.h @@ -1103,7 +1103,9 @@ struct igammac_impl { } if ((x < one) || (x < a)) { - return (one - igamma_series_impl::run(a, x)); + // Clamp to [0,1] since 1-igamma can produce tiny negative values + // due to floating-point cancellation for extreme arguments. + return numext::mini(one, numext::maxi(zero, one - igamma_series_impl::run(a, x))); } return igammac_cf_impl::run(a, x); @@ -1155,7 +1157,9 @@ struct igamma_generic_impl { if ((x > one) && (x > a)) { Scalar ret = igammac_cf_impl::run(a, x); if (mode == VALUE) { - return one - ret; + // Clamp to [0,1] since 1-igammac can produce tiny negative values + // due to floating-point cancellation for extreme arguments. + return numext::mini(one, numext::maxi(zero, one - ret)); } else { return -ret; }