diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs index 06047b80e..013a24b83 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs @@ -79,12 +79,18 @@ 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; - if (!re.IsNegative && SquaredModulus(re, im).CompareTo(Reach(precision)) > 0) - erfc = ErfcByTheAsymptoticSeries(re, im, work); + // 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 + // 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 { + 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..e05b4bf59 100644 --- a/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs +++ b/Sources/Tests/UnitTests/Convenience/ErrorFunctionTest.cs @@ -46,6 +46,10 @@ 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(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")] @@ -80,6 +84,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)")]