Skip to content

Commit f633765

Browse files
erfc far out on the right returns at once, and keeps its real part on the imaginary axis (#1609)
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). 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". Closes #1606. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 429ace7 commit f633765

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,18 @@ public static Complex Erfc(Complex z)
7979
if (re.IsZero && im.IsZero)
8080
return Integer.One;
8181
var precision = context.Precision.ToInt32Checked();
82-
var work = Working(context, GuardDigits(re, im, precision) + (re.IsNegative ? 0 : Cancelled(re, im)));
8382
(EDecimal Re, EDecimal Im) erfc;
84-
if (!re.IsNegative && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0)
85-
erfc = ErfcByTheAsymptoticSeries(re, im, work);
83+
// The asymptotic series is erfc itself and cancels nothing, so it does without the
84+
// digits 1 - erf(z) would lose. Those grow as Re(z^2): 4343 of them at z = 100,
85+
// where erfc(100) took over 20 s. https://github.com/asc-community/AngouriMath/issues/1606
86+
// On the imaginary axis it is 1 - erf(z) instead, where erf(i y) is i erfi(y) exactly:
87+
// the asymptotic series leaves out the 1 there, below its precision beside erfi(y),
88+
// and answered erfc(20 i) with a real part of 0.
89+
if (!re.IsNegative && !re.IsZero && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0)
90+
erfc = ErfcByTheAsymptoticSeries(re, im, Working(context, GuardDigits(re, im, precision)));
8691
else
8792
{
93+
var work = Working(context, GuardDigits(re, im, precision) + (re.IsNegative ? 0 : Cancelled(re, im)));
8894
var (erfRe, erfIm) = Erf(re, im, work, extraDigits: 0);
8995
erfc = (EDecimal.One.Subtract(erfRe, work), erfIm.Negate());
9096
}

‎Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public sealed class ErrorFunctionTest
4646
[InlineData("erfc(10)", "2.08848758376254475700078629495778861156081812e-45", "0")]
4747
[InlineData("erfc(-1)", "1.842700792949714869341220635082609259296067", "0")]
4848
[InlineData("erfc(15 + 5 * i)", "4.3823936244741368125841545115653530204851946e-89", "2.25866204017490621858266035895561905217636073e-89")]
49+
[InlineData("erfc(16)", "2.32848575157153069336487285457344259753439695e-113", "0")]
50+
[InlineData("erfc(16 + 3 * i)", "-6.63976210744169686093175089357492626279041806e-110", "-1.73191535469740949550705185233821524194290283e-109")]
51+
[InlineData("erfc(14 - 9 * i)", "1.24364721743380983428549812322004008428400814e-52", "3.64858214804222874123198985548113095364186296e-52")]
52+
[InlineData("erfc(20 * i)", "1", "-1.47479753962878620244773315313183512459925928e+172")]
4953
[InlineData("erfc(2 + i)", "-0.00360634272565175091291182820541914235532928537", "0.0112590060288150250764009156316482248536651599")]
5054
[InlineData("erfi(1)", "1.65042575879754287602533772956136244389567987", "0")]
5155
[InlineData("erfi(10)", "1524307422708669699360546614726544062463812.07", "0")]
@@ -80,6 +84,23 @@ private static void Close(EDecimal expected, EDecimal actual)
8084
public void ExactValues(string expression, string expected)
8185
=> Assert.Equal(expected.ToEntity(), expression.ToEntity().InnerSimplified);
8286

87+
/// <summary>
88+
/// Far out on the right erfc is below what the default precision represents, and it is
89+
/// its asymptotic series, which cancels nothing. It was carried with the <c>Re(z^2)/ln 10</c>
90+
/// digits that <c>1 - erf(z)</c> would lose: 4343 at 100, where it took about 25 s, and
91+
/// 43430 at 1000, where it did not return in 30.
92+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1606">#1606</a>
93+
/// </summary>
94+
[Theory]
95+
[InlineData("erfc(1000)")]
96+
[InlineData("erfc(1000 + i)")]
97+
public void FarOutOnTheRightItIsZeroAtOnce(string expression)
98+
{
99+
var evaluation = System.Threading.Tasks.Task.Run(() => MathS.FromString(expression, useCache: false).EvalNumerical());
100+
Assert.True(evaluation.Wait(System.TimeSpan.FromSeconds(30)), $"{expression} did not return");
101+
Assert.True(evaluation.Result.RealPart.EDecimal.IsZero && evaluation.Result.ImaginaryPart.EDecimal.IsZero, $"{expression} = {evaluation.Result}");
102+
}
103+
83104
[Theory]
84105
[InlineData("erf(x)", "2 / sqrt(pi) * e ^ (-x ^ 2)")]
85106
[InlineData("erfc(x)", "-2 / sqrt(pi) * e ^ (-x ^ 2)")]

0 commit comments

Comments
 (0)