From 06c277bcd769e45565417ec3b5f03d1eff871c1c Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 3 Dec 2026 11:54:12 +0000 Subject: [PATCH] Keep the digits of arsinh and arcsch of a small argument arsinh(a) is ln(a + sqrt(a^2 + 1)), and the square root, within a^2/2 of 1, is taken for 1 by the downcasting below half the working digits, so the logarithm was ln(1 + a): arsinh(10^-25) kept 25 digits and arsinh(10^-60) 60. The logarithm near 1, evaluated again with more digits, kept a^2/2 only where those digits put the tolerance below it, which is why 10^-30 and 10^-35 were right. Above 10^-25 the sum near 1 kept the digits of 1, not of a. The logarithm of s a + sqrt(a^2 + 1), which arcsch reaches with a = 1/y, is now taken for a below about 10^-2 as ln(1 + u), u = s a + a^2/(sqrt(a^2 + 1) + 1), which cancels nothing, with as many digits more as u is below 1. Fixes https://github.com/asc-community/AngouriMath/issues/2686 Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 27 ++++ ...aluation.Continuous.Arithmetics.Classes.cs | 119 +++++++++++++++--- .../Common/NumericsRegressionTest.cs | 29 +++++ 3 files changed, 161 insertions(+), 14 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index dbfcaafd1..a5b073a5a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -229,6 +229,33 @@ A square root past `10^50` that the working digits do not place as an integer is 64 of them `-oo`; this branch has none wrong, and 88 `NaN`, every one of them `Ci` or `Si` of a decimal from `10^100` off the real line, and each wrong or `NaN` on master. +### arsinh and arcsch of a small argument keep all their digits + +**Silent: wrong numbers become right ones.** `arsinh(a)` is `ln(a + sqrt(a^2 + 1))`, and for a small `a` the square root +is within `a^2/2` of 1, which the downcasting rounds onto 1 below half the working digits: from `a` below `10^(-25)` the +logarithm was `ln(1 + a)`, short of `a^2/2`, except where the digits a logarithm near 1 is evaluated again with put the +tolerance back below it, as they did for `10^(-30)` and `10^(-35)`. Above `10^(-25)` the sum near 1 kept the working +digits of 1 rather than of `a`, and the logarithm lost as many digits as `a` is below 1. The logarithm is now taken as +`ln(1 + u)` with `u = a + a^2 / (sqrt(a^2 + 1) + 1)`, which is the same number and cancels nothing, for `a` below about +`10^(-2)`; `arcsch(y)` is the same logarithm with `a = 1/y`. For an imaginary `a` the answer is imaginary. Measured on +master (0b1b7d7cc) and on this branch; the true values are mpmath's at 400 digits +([#2686](https://github.com/asc-community/AngouriMath/issues/2686)). + +| `.Evaled` | Was (master) | Now | +|---|---|---| +| `arsinh(10^(-16))` | `9.99999999999999999999999999999998333…3333383333333333332882E-17`, right to 84 digits | `9.99999999999999999999999999999998333…3333333333333333332887E-17` | +| `arsinh(10^(-25))` | `9.999999999999999999999999500000…E-26`, right to 25 digits | `9.99999999999999999999999999999999999999999999999998333…E-26` | +| `arsinh(10^(-40))` | `9.999999999999999999999999999999999999999500000…E-41`, right to 40 digits | `9.999…99998333…E-41` | +| `arsinh(10^(-60))` | `9.999…9995000…E-61`, right to 60 digits | `1.000…000E-60` | +| `arcsch(10^40)` | `9.999999999999999999999999999999999999999500000…E-41`, right to 40 digits | `9.999…99998333…E-41` | +| `arcsch(10^60)` | `9.999…9995000…E-61`, right to 60 digits | `1.000…000E-60` | +| `arsinh(10^(-46) * i)` | `9.999…99966666667E-47i`, right to 92 digits | `1.000…0001666667E-46i` | + +Of 1000 sampled values of `arsinh` and `arcsch`, at `±10^(-k)`, `±10^(-k) ± 10^(-k-3)/3` and `±10^(-k) i` and their +reciprocals for `k` up to 120, and at moderate and large arguments, master had 391 wrong past a relative `1e-95`; this +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`. + ### 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 7f9325db2..7a134cbde 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 @@ -597,8 +597,9 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Base, Antilogarithm, (a, b) => (a, b) switch { - (Complex n1, Complex n2) when !isExact => Number.Log(n1, WithoutARootsCancellation(n2) ?? n2) is var logarithm && MayBeNearOne(logarithm) - ? NearOneWithMoreDigits(n1, n2) ?? logarithm : logarithm, + (Complex n1, Complex n2) when !isExact => OfASmallAreaArgument(n2) + ?? (Number.Log(n1, WithoutARootsCancellation(n2) ?? n2) is var logarithm && MayBeNearOne(logarithm) + ? NearOneWithMoreDigits(n1, n2) ?? logarithm : logarithm), // The principal logarithm of the four points of the unit circle that are // numbers, i times the argument in (-pi; pi]: ln(-1) is i pi and ln(i) is // i pi / 2. ln(1) is 0 below. A trigonometric root was written ln(-1) / i @@ -683,20 +684,110 @@ _ when IsExactArithmetic(a) && IsExactArithmetic(b) && b.Evaled is Complex bValu // Where s a + t sqrt(b) cancels, s a - t sqrt(b) is about 2 s a and cancels nothing. var conjugate = (s > 0 ? aValue : -aValue) - (t > 0 ? rootValue : -rootValue); return conjugate is { RealPart.EDecimal.IsZero: true, ImaginaryPart.EDecimal.IsZero: true } ? null : difference / conjugate; + } + + private static bool IsAHalf(Rational power) => power.ERational.Numerator.Equals(PeterO.Numbers.EInteger.One) + && power.ERational.Denominator.Equals(PeterO.Numbers.EInteger.FromInt32(2)); + + /// a^2 is the square of a, and 1/y^2 the square of 1/y, which is how arcsch writes it. + private static bool IsTheSquareOf(Entity square, Entity of) + => square is Powf(var squared, Integer(2)) && squared == of + || of is Divf(Integer(1), var below) && square is Divf(Integer(1), Powf(var belowSquared, Integer(2))) && belowSquared == below; + + /// About log10 of the larger part, from the exponent and the bits of the mantissa: within one either way. + private static long Magnitude(Complex number) + { + static long Of(PeterO.Numbers.EDecimal part) + => part.IsZero || !part.IsFinite ? long.MinValue + : part.Exponent.ToInt64Unchecked() + (long)(part.Mantissa.GetUnsignedBitLengthAsInt64() * 0.30103); + return System.Math.Max(Of(number.RealPart.EDecimal), Of(number.ImaginaryPart.EDecimal)); + } - static bool IsAHalf(Rational power) => power.ERational.Numerator.Equals(PeterO.Numbers.EInteger.One) - && power.ERational.Denominator.Equals(PeterO.Numbers.EInteger.FromInt32(2)); - // a^2 is the square of a, and 1/y^2 the square of 1/y, which is how arcsch writes it. - static bool IsTheSquareOf(Entity square, Entity of) - => square is Powf(var squared, Integer(2)) && squared == of - || of is Divf(Integer(1), var below) && square is Divf(Integer(1), Powf(var belowSquared, Integer(2))) && belowSquared == below; - // About log10 of the larger part, from the exponent and the bits of the mantissa: within one either way. - static long Magnitude(Complex number) + /// + /// 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 + /// ln(1 + u) with u = s a + a^2 / (sqrt(a^2 + 1) + 1), which is the same number and is + /// known to the working digits relative to itself; for any other logarithm. + /// + /// + /// + /// The square root is within a^2/2 of 1, and the downcasting takes a value within + /// 10^(-precision/2) of an integer for the integer, so from a below + /// 10^(-precision/4) it was 1 and the logarithm was ln(1 + a), short of a^2/2: + /// arsinh(10^(-25)) was right to 25 of a hundred digits. The logarithm near 1 is evaluated again + /// with more digits (), which kept a^2/2 where those digits put + /// the downcasting's tolerance below it -- 10^(-30) and 10^(-35) were right -- and from + /// 10^(-40) lost it again, arsinh(10^(-60)) keeping sixty digits. Above + /// 10^(-precision/4) the sum s a + sqrt(a^2 + 1) is near 1 and is rounded at the working + /// digits, so the logarithm, about a, is short of as many digits as a is below 1: + /// arsinh(10^(-16)) had 84. + /// + /// + /// u is a sum of s a and a term a times smaller and cancels nothing, the square + /// root enters it only in that smaller term, and is made exact enough there by writing + /// sqrt(a^2 + 1) - 1 once more as a^2 over itself plus 2. The logarithm of 1 + u + /// is taken with as many digits more as u is below 1. For an imaginary a below 1, + /// 1 + u is on the unit circle and its logarithm has no real part. + /// https://github.com/asc-community/AngouriMath/issues/2686 + /// + /// + private Complex? OfASmallAreaArgument(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), + Sumf(Powf(_, Rational half) radical, var term) when IsAHalf(half) => (term, radical, 1), + Minusf(Powf(_, Rational half) radical, var term) when IsAHalf(half) => (term, radical, -1), + _ => (null!, null!, 0), + }; + if (s == 0 || !(root.Base is Sumf(var square, Integer(1)) && IsTheSquareOf(square, a) + || root.Base is Sumf(Integer(1), var squareAfter) && IsTheSquareOf(squareAfter, a))) + return null; + if (!ReferenceEquals(Antilogarithm.Evaled, antilogarithm) || a.Evaled is not Complex aValue || root.Evaled is not Complex rootValue) + return null; + var size = Magnitude(aValue); + if (size == long.MinValue || size >= -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; + using (MathS.Settings.DowncastingEnabled.Set(false)) + using (MathS.Settings.DecimalPrecisionContext.Set(work)) + { + var aSquared = aValue * aValue; + // sqrt(a^2 + 1) - 1, from the root as evaluated, and once more from itself: the root the + // downcasting took for 1 leaves it short of a^4/8, and a^6/32 after. + var excess = aSquared / (rootValue + 1); + excess = aSquared / (excess + 2); + u = (s > 0 ? aValue : -aValue) + excess; + } + var (re, im) = (u.RealPart.EDecimal, u.ImaginaryPart.EDecimal); + if (!re.IsFinite || !im.IsFinite) + return null; + if (im.IsZero) + 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); + 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) { - static long Of(PeterO.Numbers.EDecimal part) - => part.IsZero || !part.IsFinite ? long.MinValue - : part.Exponent.ToInt64Unchecked() + (long)(part.Mantissa.GetUnsignedBitLengthAsInt64() * 0.30103); - return System.Math.Max(Of(number.RealPart.EDecimal), Of(number.ImaginaryPart.EDecimal)); + 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 a7946a849..fcfef8158 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -772,5 +772,34 @@ public void Issue2683_ATrigonometricIntegralOfADecimalPastItsDigitsIsNaN(string { Assert.Equal(MathS.NaN, expression.ToEntity().Evaled); } + + // arsinh(a) is ln(a + sqrt(a^2 + 1)), and for a small a the square root is within a^2/2 of 1: the downcasting + // took it for 1 from a below 10^-25, at the working digits and at those the logarithm near 1 is evaluated again + // with, and the logarithm was ln(1 + a), short of a^2/2 -- 25 digits of arsinh(10^-25), 60 of arsinh(10^-60). + // Above, the sum near 1 kept the working digits of 1 and not of a: 84 digits of arsinh(10^-16). Each part is held + // to a relative 1e-95 of mpmath's at 400 digits, and a part that is 0 to 0. + // https://github.com/asc-community/AngouriMath/issues/2686 + [Theory] + [InlineData("arsinh(10^(-25))", "9.9999999999999999999999999999999999999999999999999833333333333333333333333333333333333333333333333334083333333e-26", "0")] + [InlineData("arsinh(10^(-60))", "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-60", "0")] + [InlineData("arsinh(-10^(-40))", "-9.9999999999999999999999999999999999999999999999999999999999999999999999999999999833333333333333333333333333333e-41", "0")] + [InlineData("arsinh(10^(-16))", "9.9999999999999999999999999999999833333333333333333333333333333334083333333333333333333333333333328869047619048e-17", "0")] + [InlineData("arsinh(10^(-80))", "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-80", "0")] + [InlineData("arsinh(10^(-20) + 10^(-23)/3)", "1.0003333333333333333333333333333333333333166499944438271604938271604938271604938279117446607716512376543209877e-20", "0")] + [InlineData("arsinh(10^(-46) * i)", "0", "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016666666666666667e-46")] + [InlineData("arsinh(-10^(-30) * i)", "0", "-1.0000000000000000000000000000000000000000000000000000000000001666666666666666666666666666666666666666666666667e-30")] + [InlineData("arcsch(10^40)", "9.9999999999999999999999999999999999999999999999999999999999999999999999999999999833333333333333333333333333333e-41", "0")] + [InlineData("arcsch(-10^60)", "-1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-60", "0")] + [InlineData("arcsch(10^46 * i)", "0", "-1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016666666666666667e-46")] + public void Issue2686_TheAreaSineOfASmallArgumentKeepsItsDigits(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"); + } + } } }