diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 5a4e4ff16..4434c792b 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -256,6 +256,43 @@ reciprocals for `k` up to 120, and at moderate and large arguments, master had 3 branch has none, and 563 changed, all of them `a` below `10^(-2)`. The last digit of some printed values moves, and an imaginary value whose digits past the first are all 0 prints short: `arcsch(10^200 * i)` is `-1E-200i`. +### arcosh, arsech, artanh and arcoth near their zero keep all their digits + +**Silent: wrong numbers become right ones.** `arcosh(a)` is `ln(a + sqrt(a + 1) sqrt(a - 1))`, about +`ln(1 + sqrt(2 (a - 1)))` near 1, and the sum near 1 kept the working digits of 1 rather than of the root, so the +logarithm lost as many digits as `sqrt(a - 1)` is below 1. From `a - 1` below `10^(-100)` the sum is within `10^(-50)` of +1, the downcasting took it for 1, and the answer was 0. A complex `a` near 1 had a part within `10^(-50)` of 1 taken for 1 +the same way: `arsech(1 + 10^(-30) i)` evaluates `1/(1 + 10^(-30) i)`, whose real part `1 - 10^(-60)` was 1. `artanh(x)` +is `ln((1 + x)/(1 - x))/2`, and the quotient near 1 was rounded at the working digits, losing up to as many digits as +`x` is below 1 -- 80 of `artanh(10^(-20))`, 75 of `artanh(10^(-25))` -- until the logarithm near 1 evaluated again with +more digits took over below that. The logarithm is now taken as +`ln(1 + u)`, with `u = (a - 1) + sqrt(a + 1) sqrt(a - 1)` for `a` within about `10^(-2)` of 1, `a` evaluated again without +the downcasting where it is not rational, and with `u = 2x / (1 - x)` for `x` below about `10^(-2)`; `arsech(y)` and +`arcoth(y)` are the same logarithms of `1/y`. Near -1, `arcosh` and `arsech` are taken the same way, as `ln(-(1 + u))`. The +logarithm of a rational within about `10^(-2)` of 1 is taken from its distance from 1, which is exact. Measured on master +(70b17bba4, which differs from 3ccf893f2 by #2688 only) and on this branch; the true values are mpmath's at 700 digits +([#2689](https://github.com/asc-community/AngouriMath/issues/2689)). + +| `.Evaled` | Was (master) | Now | +|---|---|---| +| `arcosh(1 + 10^(-20))` | `1.414213562373095048800510…875687239457891844E-10`, right to 89 digits | `1.414213562373095048800510…875687241607101855E-10` | +| `arcosh(1 + 10^(-40))` | `1.414213562373095048801688…644977927549584108301E-20`, right to 79 digits | `1.414213562373095048801688…625183257769371987089E-20` | +| `arcosh(1 + 10^(-120))` | `0` | `1.414213562373095048801688724209698…E-60` | +| `arsech(1 - 10^(-120))` | `0` | `1.414213562373095048801688724209698…E-60` | +| `arsech(1 + 10^(-30) * i)` | `1.000000000000000000000000000000083333…E-15 - 9.999…99916666…i`, right to 30 digits | `9.99999999999999999999999999999583333…E-16 - 1.000000000000000000000000000000416666…E-15i` | +| `artanh(10^(-20))` | `1.000…0333…3333325333333333333333334E-20`, right to 80 digits | `1.000…0333…3333335333333333333333333E-20` | +| `arcoth(10^20)` | `1.000…0333…3333325333333333333333334E-20`, right to 80 digits | `1.000…0333…3333335333333333333333333E-20` | +| `arcosh(-1 - 10^(-120))` | `3.14159…i`, the real part `1.414…E-60` missing | `1.414213562373095048801688724209698…E-60 + 3.14159…i` | + +Of 4059 sampled values of the four functions and of the logarithm of a rational near 1, at `1 ± 10^(-k)`, `1 + 10^(-k) + 10^(-k-3)/3`, `1 ± 10^(-k) i` and +`1 + 10^(-k) (±1 + i)` for `arcosh` and `arsech`, at `-1 - 10^(-k)` and `-1 + 10^(-k)`, at `±10^(-k)`, `±10^(-k) i` and +`±10^(-k) (1 + i)` for `artanh` and their reciprocals for `arcoth`, for `k` from 1 to 240, and at moderate, large and +branch-cut arguments, master had 1204 wrong past a relative `1e-95`; this branch has 11, which are `artanh` of a real above +1 and `arcoth` of a real between -1 and 1 and are master's values unchanged: their imaginary part is `pi/2` with the sign +the logarithm of a negative number gives, where mpmath takes the other side of the cut. `artanh(1)` and `arcoth(1)` are +NaN and `artanh(-1)` and `arcoth(-1)` are `-oo`, on both. The last digit of some printed +values moves, and a real part that is 0 on the unit circle, `arcosh(1 - 10^(-30))` among them, stays 0. + ### A number past the working digits is not taken for an exact one **Not silent: wrong answers become exact ones.** At the default hundred digits, a product past about `10^50` diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs index 7a134cbde..273be0a45 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs @@ -703,6 +703,37 @@ static long Of(PeterO.Numbers.EDecimal part) return System.Math.Max(Of(number.RealPart.EDecimal), Of(number.ImaginaryPart.EDecimal)); } + /// + /// The natural logarithm of an area function's argument near the point where the function is 0, as + /// ln(1 + u) with u known to the working digits relative to itself; + /// for any other logarithm. The arguments are s a + sqrt(a^2 + 1) for an a near 0 + /// (arsinh(a), and arcsch(y) with a = 1/y), a + sqrt(a + 1) sqrt(a - 1) for an + /// a near 1, and near -1 (arcosh(a), and arsech(y) with a = 1/y), (p + q) / (p - q) + /// for a q/p near 0 (artanh and arcoth), and an exact rational near 1, which is what + /// the last of these is where p and q are rational. + /// + /// + /// Each argument is within its logarithm of 1, and at the working digits it keeps only the digits of + /// that distance above them, so the logarithm is short of as many digits as it is below 1; below half + /// the digits the downcasting takes the argument for 1 itself, and the logarithm was 0. Written as + /// 1 + u, with u made from the parts without the cancellation, the logarithm is taken with + /// as many digits more as u is below 1. + /// https://github.com/asc-community/AngouriMath/issues/2686 + /// https://github.com/asc-community/AngouriMath/issues/2689 + /// + private Complex? OfASmallAreaArgument(Complex antilogarithm) + { + if (Base is not Variable { Name: "e" }) + return null; + return (Antilogarithm switch + { + Sumf(_, Mulf(Powf, Powf)) => OfAnArcoshNearOneOrMinusOne(antilogarithm), + Sumf or Minusf => OfAnArsinhNearZero(antilogarithm), + Divf(Sumf, Minusf) => OfAQuotientNearOne(antilogarithm), + _ => null, + }) ?? (antilogarithm is Rational rational and not Integer ? OfARationalNearOne(rational) : null); + } + /// /// The natural logarithm of s a + sqrt(a^2 + 1), with s 1 or -1 -- arsinh(a), and /// arcsch(y) where a is 1/y -- for an a below about 10^(-2), as @@ -731,10 +762,8 @@ static long Of(PeterO.Numbers.EDecimal part) /// https://github.com/asc-community/AngouriMath/issues/2686 /// /// - private Complex? OfASmallAreaArgument(Complex antilogarithm) + private Complex? OfAnArsinhNearZero(Complex antilogarithm) { - if (Base is not Variable { Name: "e" }) - return null; var (a, root, s) = Antilogarithm switch { Sumf(var term, Powf(_, Rational half) radical) when IsAHalf(half) => (term, radical, 1), @@ -765,30 +794,216 @@ static long Of(PeterO.Numbers.EDecimal part) excess = aSquared / (excess + 2); u = (s > 0 ? aValue : -aValue) + excess; } + return OneAndLn(u, aValue.RealPart.EDecimal.IsZero ? PeterO.Numbers.EDecimal.Zero : null, context, work); + } + + /// + /// The natural logarithm of a + sqrt(a + 1) sqrt(a - 1) -- arcosh(a), and arsech(y) + /// where a is 1/y -- for an a within about 10^(-2) of 1 or of -1, as + /// ln(1 + u) with u = (a - 1) + sqrt(a + 1) sqrt(a - 1) near 1, and as ln(-(1 + u)) with + /// u = -(a + 1) - sqrt(a + 1) sqrt(a - 1) near -1, the distance taken from a before it is + /// rounded; for any other logarithm. + /// + /// + /// The argument is about 1 + sqrt(2 (a - 1)), so at the working digits the logarithm was short of + /// as many digits as sqrt(a - 1) is below 1 -- arcosh(1 + 10^(-40)) had 79 of a hundred -- + /// and from a - 1 below 10^(-precision) the argument is within 10^(-precision/2) of 1, + /// and the downcasting took it for 1: + /// arcosh(1 + 10^(-120)) was 0 where it is about 1.414 * 10^(-60). Near -1 the argument is + /// about -1 - sqrt(2 (-1 - a)), and the logarithm was right only while the logarithm near 1 evaluated + /// again with more digits () could place it: arcosh(-1 - 10^(-120)) + /// had 60 digits. Both roots are taken again with twelve digits more, so that the root of the distance is + /// the root of the distance and not of what the working digits left of it. For a real a in + /// [-1, 1] the argument is on the unit circle, and its logarithm, i arccos(a), has no real + /// part. https://github.com/asc-community/AngouriMath/issues/2689 + /// + private Complex? OfAnArcoshNearOneOrMinusOne(Complex antilogarithm) + { + if (Antilogarithm is not Sumf(var a, Mulf(Powf(var first, Rational firstHalf), Powf(var second, Rational secondHalf))) + || !IsAHalf(firstHalf) || !IsAHalf(secondHalf) + || !(IsAPlusOne(first, a) && second is Minusf(var below, Integer(1)) && below == a + || IsAPlusOne(second, a) && first is Minusf(var belowFirst, Integer(1)) && belowFirst == a)) + return null; + if (!ReferenceEquals(Antilogarithm.Evaled, antilogarithm) || a.Evaled is not Complex aValue || !IsNearOneOrMinusOne(aValue)) + return null; + var context = MathS.Settings.DecimalPrecisionContext.Value; + if (context.Precision.IsZero || !context.Precision.CanFitInInt32()) + return null; + var precision = context.Precision.ToInt32Unchecked(); + var work = InternalAMExtensions.WithGuardDigits(context, 12); + // Near -1 the distance is a + 1, and the argument is -(1 + u). + var nearMinusOne = aValue.RealPart.EDecimal.IsNegative; + var one = nearMinusOne ? Integer.MinusOne : Integer.One; + Complex distance; + using (MathS.Settings.DowncastingEnabled.Set(false)) + using (MathS.Settings.DecimalPrecisionContext.Set(work)) + distance = aValue - one; + var size = Magnitude(distance); + if (size == long.MinValue || size >= -2) + return null; + // The distance has the digits of a below it only, and the downcasting took a part within + // 10^(-precision/2) of an integer for the integer: 1 + 10^(-60) (1 + i) was 1 + 10^(-60) i. Unless a + // is rational, and the distance exact, a is evaluated again without the downcasting, with as many + // digits more as the distance is below 1, and once more where those digits placed it further below. + // The downcasting goes in v3, and this with it: + // https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + var wide = work; + for (var again = 0; aValue is not Rational && again < 2 && -size + 12 > wide.Precision.ToInt32Unchecked() - precision; again++) + { + wide = Wider(context, precision + (int)System.Math.Min(-size, 20L * precision) + 20); + using var noDowncasting = MathS.Settings.DowncastingEnabled.Set(false); + using var wider = MathS.Settings.DecimalPrecisionContext.Set(wide); + if (a.Evaled is not Complex wideValue) + return null; + aValue = wideValue; + distance = aValue - one; + size = Magnitude(distance); + if (size == long.MinValue || size >= -2) + return null; + } + Complex u; + using (MathS.Settings.DowncastingEnabled.Set(false)) + using (MathS.Settings.DecimalPrecisionContext.Set(wide)) + u = nearMinusOne + ? -distance - Number.Sqrt(distance) * Number.Sqrt(aValue - Integer.One) + : distance + Number.Sqrt(aValue + Integer.One) * Number.Sqrt(distance); + // On the unit circle where a is real and between -1 and 1. + var onTheCircle = distance is Real { EDecimal: var real } && real.IsNegative != nearMinusOne; + return OneAndLn(u, onTheCircle ? PeterO.Numbers.EDecimal.Zero : null, context, wide, negated: nearMinusOne); + + static bool IsAPlusOne(Entity sum, Entity a) + => sum is Sumf(var left, Integer(1)) && left == a || sum is Sumf(Integer(1), var right) && right == a; + + // Within about a hundredth of 1 or of -1, from the parts as they are: subtracting at the working digits + // with guard digits, which a rational is written out to, was 3 kB on each arsech(0.37). + static bool IsNearOneOrMinusOne(Complex value) + { + if (value is Rational { ERational: var ratio }) + return IsWithinAHundredthOfOne(ratio.Numerator.Abs(), ratio.Denominator); + var (re, im) = (value.RealPart.EDecimal, value.ImaginaryPart.EDecimal); + if (!re.IsFinite || !im.IsFinite || !im.IsZero && Magnitude(Real.CreateWithoutDowncasting(im)) >= -2) + return false; + return re.Abs().Subtract(PeterO.Numbers.EDecimal.One, Rough).Abs().CompareTo(Hundredth) < 0; + } + } + + /// Whether |numerator / denominator - 1| is below a hundredth, in integers. + private static bool IsWithinAHundredthOfOne(PeterO.Numbers.EInteger numerator, PeterO.Numbers.EInteger denominator) + => denominator.Sign > 0 && numerator.Subtract(denominator).Abs().Multiply(100).CompareTo(denominator) < 0; + + [ConstantField] private static readonly PeterO.Numbers.EContext Rough = PeterO.Numbers.EContext.ForPrecision(8); + [ConstantField] private static readonly PeterO.Numbers.EDecimal Hundredth = PeterO.Numbers.EDecimal.Create(1, -2); + + /// + /// The natural logarithm of (p + q) / (p - q) -- artanh(x), with p = 1 and + /// q = x, and arcoth(x), with p = x and q = 1 -- for a q/p below + /// about 10^(-2), as ln(1 + u) with u = 2q / (p - q), which cancels nothing; + /// for any other logarithm. + /// + /// + /// The quotient is within 2q/p of 1 and was rounded at the working digits, so the logarithm was + /// short of as many digits as q/p is below 1: artanh(10^(-20)) had 80 of a hundred. The + /// real part of the logarithm, for a complex u, is half of ln(1 + w) with + /// w = |1 + u|^2 - 1 = 4 Re(p conj(q)) / |p - q|^2, which is 0 where q/p is imaginary. + /// https://github.com/asc-community/AngouriMath/issues/2689 + /// + private Complex? OfAQuotientNearOne(Complex antilogarithm) + { + if (Antilogarithm is not Divf(Sumf(var left, var right), Minusf(var p, var q)) + || !(left == p && right == q || left == q && right == p)) + return null; + if (!ReferenceEquals(Antilogarithm.Evaled, antilogarithm) || p.Evaled is not Complex pValue || q.Evaled is not Complex qValue) + return null; + var (pSize, qSize) = (Magnitude(pValue), Magnitude(qValue)); + if (pSize == long.MinValue || qSize == long.MinValue || qSize - pSize >= -2) + return null; + var context = MathS.Settings.DecimalPrecisionContext.Value; + if (context.Precision.IsZero || !context.Precision.CanFitInInt32()) + return null; + var work = InternalAMExtensions.WithGuardDigits(context, 12); + Complex u; + Complex difference; + using (MathS.Settings.DowncastingEnabled.Set(false)) + using (MathS.Settings.DecimalPrecisionContext.Set(work)) + { + difference = pValue - qValue; + u = Integer.Create(2) * qValue / difference; + } + if (u.ImaginaryPart.EDecimal.IsZero) + return OneAndLn(u, null, context, work); + var (pRe, pIm, qRe, qIm) = (pValue.RealPart.EDecimal, pValue.ImaginaryPart.EDecimal, qValue.RealPart.EDecimal, qValue.ImaginaryPart.EDecimal); + var (dRe, dIm) = (difference.RealPart.EDecimal, difference.ImaginaryPart.EDecimal); + var w = pRe.Multiply(qRe, work).Add(pIm.Multiply(qIm, work), work).Multiply(4, work) + .Divide(dRe.Multiply(dRe, work).Add(dIm.Multiply(dIm, work), work), work); + return OneAndLn(u, w, context, work); + } + + /// + /// The natural logarithm of a rational within about 10^(-2) of 1, from its distance from 1, which + /// is exact; for any other rational. Rounded to the working digits first, a + /// rational that is not a terminating decimal kept only the digits of the distance above them: + /// ln(100000000000000000001/99999999999999999999), which artanh(10^(-20)) evaluates + /// through, had 80 of a hundred. https://github.com/asc-community/AngouriMath/issues/2689 + /// + private static Complex? OfARationalNearOne(Rational rational) + { + var ratio = rational.ERational; + if (ratio.Numerator.Sign <= 0 + || System.Math.Abs(ratio.Numerator.GetUnsignedBitLengthAsInt64() - ratio.Denominator.GetUnsignedBitLengthAsInt64()) > 1 + || !IsWithinAHundredthOfOne(ratio.Numerator, ratio.Denominator)) + return null; + var context = MathS.Settings.DecimalPrecisionContext.Value; + if (context.Precision.IsZero || !context.Precision.CanFitInInt32()) + return null; + var work = InternalAMExtensions.WithGuardDigits(context, 12); + var distance = ratio.Subtract(PeterO.Numbers.ERational.One).ToEDecimal(work); + var size = Magnitude(Real.CreateWithoutDowncasting(distance)); + if (size == long.MinValue || size >= -2) + return null; + return Real.CreateWithoutDowncasting(OneAndLn(distance, context)); + } + + /// + /// ln(1 + u) for a u below about 10^(-2), to the digits of the context relative to + /// itself. is |1 + u|^2 - 1 where it is known without the + /// cancellation taking it from u has -- 0 on the unit circle -- and to take it + /// from u, as 2 Re u + |u|^2. asks for ln(-(1 + u)), which + /// has the same real part. + /// + private static Complex? OneAndLn(Complex u, PeterO.Numbers.EDecimal? modulusExcess, PeterO.Numbers.EContext context, PeterO.Numbers.EContext work, bool negated = false) + { var (re, im) = (u.RealPart.EDecimal, u.ImaginaryPart.EDecimal); if (!re.IsFinite || !im.IsFinite) return null; - if (im.IsZero) + if (im.IsZero && !negated) return Real.CreateWithoutDowncasting(OneAndLn(re, context)); // ln |1 + u| is half of ln(1 + w), w = |1 + u|^2 - 1 = 2 re + re^2 + im^2. - var lnModulus = aValue.RealPart.EDecimal.IsZero ? PeterO.Numbers.EDecimal.Zero - : OneAndLn(re.Multiply(2, work).Add(re.Multiply(re, work), work).Add(im.Multiply(im, work), work), context).Divide(2, context); - var argument = im.Arctan2(PeterO.Numbers.EDecimal.One.Add(re, work), work).RoundToPrecision(context); + var w = im.IsZero ? null : modulusExcess ?? re.Multiply(2, work).Add(re.Multiply(re, work), work).Add(im.Multiply(im, work), work); + if (w is { IsFinite: false }) + return null; + var lnModulus = w is null ? OneAndLn(re, context) : OneAndLn(w, context).Divide(2, context); + // The argument of -(1 + u) is pi on the positive real line, the upper side of the cut. + var argument = negated + ? (im.IsZero ? PeterO.Numbers.EDecimal.Zero : im.Negate()).Arctan2(PeterO.Numbers.EDecimal.One.Add(re, work).Negate(), work).RoundToPrecision(context) + : im.Arctan2(PeterO.Numbers.EDecimal.One.Add(re, work), work).RoundToPrecision(context); return Complex.CreateAtScales(lnModulus, argument, PeterO.Numbers.EDecimal.Zero, PeterO.Numbers.EDecimal.Zero); + } - // ln(1 + w) for a w below 1, to the digits of the context relative to itself: 1 + w taken with as - // many digits more as w is below 1, and w - w^2/2 past half the digits, where the rest is below them. - static PeterO.Numbers.EDecimal OneAndLn(PeterO.Numbers.EDecimal w, PeterO.Numbers.EContext context) - { - if (w.IsZero) - return w; - var precision = context.Precision.ToInt32Unchecked(); - var below = -(w.Exponent.ToInt64Unchecked() + w.UnsignedMantissa.GetDigitCountAsEInteger().ToInt64Unchecked()); - if (below > precision / 2 + 5) - return w.Subtract(w.Multiply(w).Divide(2, context), context); - var wide = Wider(context, precision + (int)System.Math.Max(below, 0) + 10); - return PeterO.Numbers.EDecimal.One.Add(w, wide).NaturalLogarithm(wide).RoundToPrecision(context); - } + /// + /// ln(1 + w) for a w below 1, to the digits of the context relative to itself: 1 + w + /// taken with as many digits more as w is below 1, and w - w^2/2 past half the digits, where + /// the rest is below them. + /// + private static PeterO.Numbers.EDecimal OneAndLn(PeterO.Numbers.EDecimal w, PeterO.Numbers.EContext context) + { + if (w.IsZero) + return w; + var precision = context.Precision.ToInt32Unchecked(); + var below = -(w.Exponent.ToInt64Unchecked() + w.UnsignedMantissa.GetDigitCountAsEInteger().ToInt64Unchecked()); + if (below > precision / 2 + 5) + return w.Subtract(w.Multiply(w).Divide(2, context), context); + var wide = Wider(context, precision + (int)System.Math.Max(below, 0) + 10); + return PeterO.Numbers.EDecimal.One.Add(w, wide).NaturalLogarithm(wide).RoundToPrecision(context); } /// diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs index fcfef8158..3dd11b0ef 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -801,5 +801,41 @@ public void Issue2686_TheAreaSineOfASmallArgumentKeepsItsDigits(string expressio $"{expression} is {value}, where it is {trueReal} + {trueImaginary}i"); } } + + // arcosh(a) is ln(a + sqrt(a + 1) sqrt(a - 1)), about ln(1 + sqrt(2 (a - 1))) near 1, and the sum near 1 kept the + // working digits of 1 and not of the root: 79 digits of arcosh(1 + 10^-40), and from a - 1 below 10^-100 the + // downcasting took the sum, within 10^-50 of 1, for 1, so arcosh(1 + 10^-120) was 0; a complex a had a part within + // 10^-50 of 1 taken for 1 the same way. artanh(x) is ln((1 + x)/(1 - x))/2, and the quotient near 1 was rounded at + // the working digits: 80 digits of artanh(10^-20) and of arcoth(10^20). arsech and arcoth are arcosh and artanh + // of 1/y. Each part is held to a relative 1e-95 of mpmath's at 400 digits, and a part that is 0 to 0. Evaluating + // a again without the downcasting goes with it in v3: + // https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + // https://github.com/asc-community/AngouriMath/issues/2689 + [Theory] + [InlineData("arcosh(1 + 10^(-120))", "1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462e-60", "0")] + [InlineData("arcosh(1 + 10^(-40))", "1.4142135623730950488016887242096980785696600902639282973846063905846973976447856251832577693719870887208472259e-20", "0")] + [InlineData("arcosh(1 + 10^(-20) + 10^(-23)/3)", "1.4144492449949084923380567067609658604188096989890615183166233125380147703274416186580188323505765302883744658e-10", "0")] + [InlineData("arcosh(1 - 10^(-30))", "0", "1.4142135623730950488016887242098159296998696332976815472370305726804509122805872828881491699122049237105474983e-15")] + [InlineData("arcosh(1 + 10^(-60) * (1 + i))", "1.553773974030037307344158953063146948164583499410307836332671072485365163084176218668311582372675171127820653e-30", "6.4359425290558262473544343741820980892420274244400765115615182623805593902017551291160170648233198577274128682e-31")] + [InlineData("arcosh(-1 - 10^(-120))", "1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462e-60", "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865")] + [InlineData("arsech(1 - 10^(-120))", "1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462e-60", "0")] + [InlineData("arsech(1 - 10^(-40))", "1.4142135623730950488016887242096980785697308009420469521370464750209078825487141123125605227083835447119685731e-20", "0")] + [InlineData("arsech(1 + 10^(-30) * i)", "9.9999999999999999999999999999958333333333333333333333333333306458333333333333333333333333353087797619047619048e-16", "-1.0000000000000000000000000000004166666666666666666666666666663979166666666666666666666666664691220238095238095e-15")] + [InlineData("artanh(10^(-20))", "1.0000000000000000000000000000000000000000333333333333333333333333333333333333333353333333333333333333333333333e-20", "0")] + [InlineData("artanh(-10^(-40) + 10^(-43)/3)", "-9.9966666666666666666666666666666666666666666666666666666666666666666666666666666999666777765432098765432098765e-41", "0")] + [InlineData("artanh(10^(-30) * (1 + i))", "9.9999999999999999999999999999999999999999999999999999999999933333333333333333333333333333333333333333333333333e-31", "1.0000000000000000000000000000000000000000000000000000000000006666666666666666666666666666666666666666666666667e-30")] + [InlineData("arcoth(10^20)", "1.0000000000000000000000000000000000000000333333333333333333333333333333333333333353333333333333333333333333333e-20", "0")] + [InlineData("arcoth(-10^40 * i)", "0", "9.9999999999999999999999999999999999999999999999999999999999999999999999999999999666666666666666666666666666667e-41")] + [InlineData("ln(100000000000000000001/99999999999999999999)", "2.0000000000000000000000000000000000000000666666666666666666666666666666666666666706666666666666666666666666667e-20", "0")] + public void Issue2689_TheAreaFunctionsNearTheirZeroKeepTheirDigits(string expression, string trueReal, string trueImaginary) + { + var value = Assert.IsAssignableFrom(expression.ToEntity().Evaled); + foreach (var (part, truth) in new[] { (value.RealPart.EDecimal, trueReal), (value.ImaginaryPart.EDecimal, trueImaginary) }) + { + var expected = EDecimal.FromString(truth); + Assert.True(part.IsFinite && part.Subtract(expected).Abs().CompareTo(expected.Abs().Multiply(EDecimal.FromString("1E-95"))) <= 0, + $"{expression} is {value}, where it is {trueReal} + {trueImaginary}i"); + } + } } }