From b29be05d69cbb4884684d9b51827294bd870f22b Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Wed, 30 Sep 2026 06:39:49 +0000 Subject: [PATCH 1/2] erfc far out on the right returns at once: its asymptotic series is not carried with the digits 1 - erf(z) would cancel Number.Erfc worked out one working precision for both of its branches, with Re(z^2)/ln 10 digits over for the cancellation in 1 - erf(z). Only the series branch forms that difference. Past the series' reach erfc is its asymptotic series, which cancels nothing, and the digits still grew as Re(z^2): 4343 at 100, and 43430, the cap, at 1000. erfc(70) took 5.3 s, erfc(100) over 20 s, and erfc(1000) did not return. Each is below what the default precision represents, and answers 0. The asymptotic branch now works at the guard digits alone, as the method's documentation already said it did. erfc(70) takes 9 ms, erfc(100) 3 ms, erfc(1000) 3 ms. No value changes. Fourteen arguments in the asymptotic branch, evaluated to 100 digits with downcasting off, are identical to master's to the last digit. Three new rows agree with mpmath at 200 digits to 40 digits, and mpmath gives the same values at 400. The new termination test fails on master for erfc(1000) and erfc(1000 + i). Closes #1606. Co-Authored-By: Claude Opus 5.5 --- .../Continuous/Number/ErrorFunctions.cs | 7 +++++-- .../Convenience/ErrorFunctionTest.cs | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs index 06047b80e..29489f774 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs @@ -79,12 +79,15 @@ public static Complex Erfc(Complex z) if (re.IsZero && im.IsZero) return Integer.One; var precision = context.Precision.ToInt32Checked(); - var work = Working(context, GuardDigits(re, im, precision) + (re.IsNegative ? 0 : Cancelled(re, im))); (EDecimal Re, EDecimal Im) erfc; + // The asymptotic series is erfc itself and cancels nothing, so it does without the + // digits 1 - erf(z) would lose. Those grow as Re(z^2): 4343 of them at z = 100, + // where erfc(100) took over 20 s. https://github.com/asc-community/AngouriMath/issues/1606 if (!re.IsNegative && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0) - erfc = ErfcByTheAsymptoticSeries(re, im, work); + erfc = ErfcByTheAsymptoticSeries(re, im, Working(context, GuardDigits(re, im, precision))); else { + var work = Working(context, GuardDigits(re, im, precision) + (re.IsNegative ? 0 : Cancelled(re, im))); var (erfRe, erfIm) = Erf(re, im, work, extraDigits: 0); erfc = (EDecimal.One.Subtract(erfRe, work), erfIm.Negate()); } diff --git a/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs b/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs index cb51f357e..c94bf26bb 100644 --- a/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs +++ b/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs @@ -46,6 +46,9 @@ public sealed class ErrorFunctionTest [InlineData("erfc(10)", "2.08848758376254475700078629495778861156081812e-45", "0")] [InlineData("erfc(-1)", "1.842700792949714869341220635082609259296067", "0")] [InlineData("erfc(15 + 5 * i)", "4.3823936244741368125841545115653530204851946e-89", "2.25866204017490621858266035895561905217636073e-89")] + [InlineData("erfc(16)", "2.32848575157153069336487285457344259753439695e-113", "0")] + [InlineData("erfc(16 + 3 * i)", "-6.63976210744169686093175089357492626279041806e-110", "-1.73191535469740949550705185233821524194290283e-109")] + [InlineData("erfc(14 - 9 * i)", "1.24364721743380983428549812322004008428400814e-52", "3.64858214804222874123198985548113095364186296e-52")] [InlineData("erfc(2 + i)", "-0.00360634272565175091291182820541914235532928537", "0.0112590060288150250764009156316482248536651599")] [InlineData("erfi(1)", "1.65042575879754287602533772956136244389567987", "0")] [InlineData("erfi(10)", "1524307422708669699360546614726544062463812.07", "0")] @@ -80,6 +83,23 @@ private static void Close(EDecimal expected, EDecimal actual) public void ExactValues(string expression, string expected) => Assert.Equal(expected.ToEntity(), expression.ToEntity().InnerSimplified); + /// + /// Far out on the right erfc is below what the default precision represents, and it is + /// its asymptotic series, which cancels nothing. It was carried with the Re(z^2)/ln 10 + /// digits that 1 - erf(z) would lose: 4343 at 100, where it took about 25 s, and + /// 43430 at 1000, where it did not return in 30. + /// #1606 + /// + [Theory] + [InlineData("erfc(1000)")] + [InlineData("erfc(1000 + i)")] + public void FarOutOnTheRightItIsZeroAtOnce(string expression) + { + var evaluation = System.Threading.Tasks.Task.Run(() => MathS.FromString(expression, useCache: false).EvalNumerical()); + Assert.True(evaluation.Wait(System.TimeSpan.FromSeconds(30)), $"{expression} did not return"); + Assert.True(evaluation.Result.RealPart.EDecimal.IsZero && evaluation.Result.ImaginaryPart.EDecimal.IsZero, $"{expression} = {evaluation.Result}"); + } + [Theory] [InlineData("erf(x)", "2 / sqrt(pi) * e ^ (-x ^ 2)")] [InlineData("erfc(x)", "-2 / sqrt(pi) * e ^ (-x ^ 2)")] From 0f1946360465afd2fdf2424c5467bd12e5e87443 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Wed, 30 Sep 2026 07:02:14 +0000 Subject: [PATCH 2/2] erfc on the imaginary axis keeps its real part of 1 past the series' reach erfc(i y) is 1 - i erfi(y) exactly. Past the series' reach the asymptotic series gave erfc directly, and it leaves the 1 out, below its precision beside erfi(y): erfc(20 i) came back with a real part of 0. On the axis erfc is 1 - erf(z) instead, as erf already reads erf(i y) as i erfi(y), and Cancelled adds nothing there. Found comparing a double-precision erfc against the kernel for #1607. ErrorFunctionTest gains erfc(20 i), against mpmath at 200 digits. It fails without this with "expected 1, got 0E-199". Co-Authored-By: Claude Opus 5.5 --- .../Core/Entity/Continuous/Number/ErrorFunctions.cs | 5 ++++- Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs index 29489f774..013a24b83 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs @@ -83,7 +83,10 @@ public static Complex Erfc(Complex z) // The asymptotic series is erfc itself and cancels nothing, so it does without the // digits 1 - erf(z) would lose. Those grow as Re(z^2): 4343 of them at z = 100, // where erfc(100) took over 20 s. https://github.com/asc-community/AngouriMath/issues/1606 - if (!re.IsNegative && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0) + // On the imaginary axis it is 1 - erf(z) instead, where erf(i y) is i erfi(y) exactly: + // the asymptotic series leaves out the 1 there, below its precision beside erfi(y), + // and answered erfc(20 i) with a real part of 0. + if (!re.IsNegative && !re.IsZero && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0) erfc = ErfcByTheAsymptoticSeries(re, im, Working(context, GuardDigits(re, im, precision))); else { diff --git a/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs b/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs index c94bf26bb..e05b4bf59 100644 --- a/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs +++ b/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs @@ -49,6 +49,7 @@ public sealed class ErrorFunctionTest [InlineData("erfc(16)", "2.32848575157153069336487285457344259753439695e-113", "0")] [InlineData("erfc(16 + 3 * i)", "-6.63976210744169686093175089357492626279041806e-110", "-1.73191535469740949550705185233821524194290283e-109")] [InlineData("erfc(14 - 9 * i)", "1.24364721743380983428549812322004008428400814e-52", "3.64858214804222874123198985548113095364186296e-52")] + [InlineData("erfc(20 * i)", "1", "-1.47479753962878620244773315313183512459925928e+172")] [InlineData("erfc(2 + i)", "-0.00360634272565175091291182820541914235532928537", "0.0112590060288150250764009156316482248536651599")] [InlineData("erfi(1)", "1.65042575879754287602533772956136244389567987", "0")] [InlineData("erfi(10)", "1524307422708669699360546614726544062463812.07", "0")]