From 4532fac59c0f6aac0f3ac85c2b09ebb943102d8b Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 1 Dec 2026 20:55:55 +0000 Subject: [PATCH 1/4] Keep the small parts of a logarithm near 1, a root and Ci (#2677) A logarithm took ln(1 + t) for the residual of a cancellation against 1 below half the working digits, so ln(1 + 10^(-60)) was 0, and with it arcoth, arcsch, artanh and arsech past 10^50, which are logarithms of 1 + 1/x. A logarithm whose argument's modulus is within 10^(-25) of 1, or is a 0 or 1 an inexact expression evaluated to, now evaluates its arguments again with the digits it cancels; a residual 1 stays 0. A root of a number with a decimal imaginary part other than 0 is no longer searched for among the reals, which read a phase of 10^(-60) as 0: sqrt(1 + 10^(-60) i) was 1. Ci and Si past the series measured the constant pi/2 branch terms they add and take away as terms of their own size, so Ci(10^60 + i) lost its imaginary part; a constant is a residual only against its last digits. AComplexFunctionKeepsASmallPartTest pinned the real part of ln(1 + 10^(-60) i) as 0; it is 5e-121, and the test now says so. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 37 ++++ .../Continuous/Number/ExponentialIntegrals.cs | 15 +- .../Entity/Continuous/Number/Operators.cs | 6 + .../Number/TrigonometricIntegrals.cs | 15 +- ...aluation.Continuous.Arithmetics.Classes.cs | 160 +++++++++++++++++- .../AComplexFunctionKeepsASmallPartTest.cs | 4 +- .../Common/NumericsRegressionTest.cs | 38 +++++ 7 files changed, 266 insertions(+), 9 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 16c2f63fb..0a266426a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -149,6 +149,43 @@ from below the axis as the library takes it ([#2675](https://github.com/asc-comm A decimal past `10^(precision - 16)` still has no sine to give, and `10^100 * i` is such a decimal, its imaginary part being past the digits that make it an integer: `tanh(10^100 * i)` and `Ei(10^100 * i)` stay `NaN`. +### A logarithm near 1, a root of a number just off the real line, and Ci keep their small parts + +**Silent: wrong numbers become right ones.** `ln(1 + t)` is about `t`, and a logarithm took itself for the residual of +a cancellation against 1 below half the working digits, so `ln(1 + 10^(-60))` was 0. The area functions are logarithms +of `1 + 1/x` far out, so `arcoth`, `arcsch`, `artanh` and `arsech` past `10^50` lost the part that is `1/x`. A logarithm +whose argument has a modulus within `10^(-25)` of 1, or is a 0 or a 1 that an expression other than exact arithmetic +evaluated to, now evaluates its argument again with the digits the logarithm cancels, so `arsinh(-10^60 i)`, whose +argument cancels to `-5 * 10^(-61) i`, is no longer `ln(0)`. A logarithm of a 1 that is a residual is still 0. A root of +a number with a decimal imaginary part other than 0 is no longer searched for among the reals, which took a phase of +`10^(-60)` for 0, so `(-8 + 10^(-90) i)^(1/3)` is now the principal root `1 + 1.73 i` where it was the real `-2`. And `Ci` +and `Si` past the series took the constants `pi/2` they add and take away for terms that cancel, so `Ci(10^60 + i)` had no +imaginary part. Measured on master (e3b3a34ed) and on this branch; the true values are mpmath's at 1200 digits +([#2677](https://github.com/asc-community/AngouriMath/issues/2677)). + +| `.Evaled` | Was (master) | Now | +|---|---|---| +| `arcoth(10^60)` | `0` | `1E-60` | +| `arcsch(10^60)` | `0` | `1E-60` | +| `artanh(10^200)` | `1.5707963267…i` | `1E-200 + 1.5707963267…i` | +| `arsech(10^50 * i)` | `-1.5707963267…i` | `1E-50 - 1.5707963267…i` | +| `arsinh(10^(-60))` | `0` | `1E-60` | +| `arsinh(-10^60 * i)` | `-oo` | `-138.84825276… - 1.5707963267…i` | +| `arcsch(10^(-60) * i)` | `-oo` | `-138.84825276… - 1.5707963267…i` | +| `arcosh(10^(-60) * i)` | `1.5707963267…i` | `1E-60 + 1.5707963267…i` | +| `ln(1 + 10^(-60))` | `0` | `1E-60` | +| `ln(1 + 10^(-200))` | `0` | `1E-200` | +| `ln(e^(10^(-60)))` | `0` | `1E-60` | +| `ln(1 + 10^(-60) * i)` | `1E-60i` | `5E-121 + 1E-60i` | +| `(1 + 10^(-50) * i)^(1/2)` | `1` | `1 + 5E-51i` | +| `(1 + 10^(-200) * i)^(1/3)` | `1` | `1 + 3.3333333333…E-201i` | +| `(-8 + 10^(-90) * i)^(1/3)` | `-2` | `1 + 1.7320508075…i` | +| `Ci(10^60 + i)` | `1.2813583660…E-60` | `1.2813583660…E-60 - 6.5480206585…E-61i` | + +The sweep in the pull request lists the rest. A distance from 1 below the working digits is looked for at twice and four +times them, and no further: `ln(1 + 10^(-200) * i)` has its real part, `5E-401`, but `ln(e^(10^(-300)))` and +`ln(sqrt(1 + 10^(-300)))` are still 0. An exact argument's distance is known, and `ln(1 + 10^(-300))` is `1E-300`. + The new `arcsin` and `arccos` also move the last digits of moderate arguments where one part is far larger than the other: of 914 sampled evaluations of the functions above, 22 changed, all of them `arcsin`, `arccos` or `arcsec`, and each is nearer mpmath's than it was. `arcsin(10^40 - 3/7 i)` had a real part wrong from its 39th digit, and now from its 100th. diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs index a5cff754b..8ea420af5 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs @@ -118,7 +118,11 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) Ei var precision = context.Precision.ToInt32Checked() + extraDigits; var modulus = Math.Sqrt(Math.Pow(re.ToDouble(), 2) + Math.Pow(im.ToDouble(), 2)); if (modulus > (precision + 5) * Math.Log(10)) - return EiByTheAsymptoticSeries(re, im, Working(context, extraDigits + 10), exactIm); + { + var asymptoticWork = Working(context, extraDigits + 10); + return EiByTheAsymptoticSeries(re, im, asymptoticWork, exactIm, + asymptoticWork.Precision.ToInt32Checked() - context.Precision.ToInt32Checked()); + } // The digits the terms cancel off the positive axis, and ten over. var cancelled = (int)Math.Ceiling((modulus - re.ToDouble()) / Math.Log(10)); var work = Working(context, extraDigits + cancelled + 10); @@ -166,7 +170,7 @@ private static ((EDecimal Re, EDecimal Im) Value, (EDecimal Re, EDecimal Im) Sca /// e^z/z sum_k k!/z^k + i pi sgn(Im z), cut at its smallest term, and for each part the sizes of the /// products added to make it. /// - private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) EiByTheAsymptoticSeries(EDecimal re, EDecimal im, EContext work, bool exactIm) + private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) EiByTheAsymptoticSeries(EDecimal re, EDecimal im, EContext work, bool exactIm, int guard) { var epsilon = EDecimal.Create(EInteger.One, EInteger.FromInt32(-work.Precision.ToInt32Checked())); // 1/z = (re - i im)/|z|^2 @@ -206,7 +210,12 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) Ei { var pi = EDecimal.PI(work); valueIm = valueIm.Add(im.IsNegative ? pi.Negate() : pi, work); - imScale = imScale.Add(pi, work); + // A constant cancels nothing: it is a residual only against its last digits, those past the + // context's. Against pi itself, Ci(10^60 + i), whose pi from Ei(z) and Ei(-z) and pi/2 from each + // branch term cancel to about -6.5 * 10^(-61) i, had no imaginary part. + // https://github.com/asc-community/AngouriMath/issues/2677; the residuals go with the downcasting in v3, + // https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + imScale = imScale.Add(pi.ScaleByPowerOfTen(-guard), work); } return (valueRe, valueIm, reScale, imScale); } diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs index 4f870fc42..9beeecd95 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs @@ -422,6 +422,12 @@ private static bool CanHaveARealRoot(Complex @base, EInteger rootPower) => // downcasting's tolerance, and sqrt(10^50 + i) was 10^25. https://github.com/asc-community/AngouriMath/issues/2675 if (@base.ImaginaryPart is Rational { ERational.IsZero: false }) return null; + // Nor is one whose imaginary part is a decimal other than 0, which a number's parts keep only where it is + // no residual of what made it: the search reads a phase of 10^(-60) as 0, and sqrt(1 + 10^(-60) i) was + // 1 where it is 1 + 5 * 10^(-61) i. https://github.com/asc-community/AngouriMath/issues/2677, and the + // search goes with the downcasting in v3: https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + if (@base.ImaginaryPart.EDecimal is { IsZero: false, IsFinite: true }) + return null; // Searched at the size of one, by an exact power of ten: the candidates are rounded with // the zero range at 1e-6, so a root below it was the integer 0, and 0 ^ q was within 1e-6 // of the base, so sqrt(1/10^20) was 0. Scaled, it is sqrt(1) / 10^10. diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs index 533c7b9ff..04af179d1 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs @@ -188,10 +188,14 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) By // -i (a + i b) = b - i a return (valueIm, valueRe.Negate(), imScale, reScale); // ln z - ln(i z) is -i pi/2 where arg z <= pi/2, and 3 i pi/2 past it. - var pi = EDecimal.PI(Working(context, 10)); + var branchWork = Working(context, 10); + var pi = EDecimal.PI(branchWork); var pastAQuarterTurn = re.IsNegative && !im.IsNegative; var branchTerm = pastAQuarterTurn ? pi.Multiply(3).Divide(2) : pi.Divide(-2); - return (valueRe, valueIm.Add(branchTerm), reScale, imScale.Add(branchTerm.Abs())); + // A constant is a residual only against its digits past the context's: see EiByTheAsymptoticSeries. + // https://github.com/asc-community/AngouriMath/issues/2677 + return (valueRe, valueIm.Add(branchTerm), reScale, + imScale.Add(branchTerm.Abs().ScaleByPowerOfTen(context.Precision.ToInt32Checked() - branchWork.Precision.ToInt32Checked()))); } var work = Working(context, 10); var (plusRe, plusIm, plusReScale, plusImScale) = Ei(re, im, context, extraDigits: 10, alongIsExact); @@ -204,17 +208,20 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) By var sumReScale = plusReScale.Add(minusReScale, work).Multiply(half, work); var sumImScale = plusImScale.Add(minusImScale, work).Multiply(half, work); var halfPi = EDecimal.PI(work).Multiply(half, work); + // The branch terms are constants, residuals only against their digits past the context's. + // https://github.com/asc-community/AngouriMath/issues/2677 + var beyond = context.Precision.ToInt32Checked() - work.Precision.ToInt32Checked(); // The branch terms of (ln z - ln(1/z))/2 in Ei(z) and Ei(-z). if (!im.IsZero) { var branch = im.IsNegative ? halfPi.Negate() : halfPi; sumIm = odd ? sumIm.Subtract(branch, work) : sumIm.Add(branch, work); - sumImScale = sumImScale.Add(halfPi, work); + sumImScale = sumImScale.Add(halfPi.ScaleByPowerOfTen(beyond), work); } else if (!odd && re.IsNegative) { sumIm = sumIm.Add(EDecimal.PI(work), work); - sumImScale = sumImScale.Add(EDecimal.PI(work), work); + sumImScale = sumImScale.Add(EDecimal.PI(work).ScaleByPowerOfTen(beyond), work); } return (sumRe, sumIm, sumReScale, sumImScale); } 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 f170f6360..e9ed2da1c 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,7 +597,7 @@ protected override Entity InnerSimplify(bool isExact) => ExpandOnTwoArguments(Base, Antilogarithm, (a, b) => (a, b) switch { - (Complex n1, Complex n2) when !isExact => Number.Log(n1, n2), + (Complex n1, Complex n2) when !isExact => NearOneWithMoreDigits(n1, n2) ?? Number.Log(n1, n2), // 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 @@ -638,6 +638,164 @@ when a.Evaled is Real below && below > 0 && below < 1 }, (@this, a, b) => ((Logf)@this).New(a, b), isExact); + /// + /// The logarithm where the modulus of the antilogarithm or of the base is within + /// 10^(-precision/4) of 1, taken from both evaluated again with the digits the + /// logarithm cancels and as many again; anywhere else. + /// + /// + /// + /// ln(1 + t) is about t, and at the working digits 1 + t keeps only + /// the digits of t above them, so the logarithm is short of as many digits as + /// t is below 1, and below half of them it was read as the residual of the + /// cancellation that made it: ln(1 + 10^(-60)) was 0, and with it every area + /// function past 10^50, which are logarithms of 1 + 1/x -- + /// arcoth(10^60) and arcsch(10^60) were 0 where they are 10^(-60). + /// A logarithm of 1 that is a residual, ln(sqrt(2)^2/2), stays one with more + /// digits, and is 0 still. https://github.com/asc-community/AngouriMath/issues/2677 + /// + /// + /// Where the distance from 1 is below the working digits it is not known, nor where an + /// expression that is not exact arithmetic evaluated to exactly 1 -- the downcasting rounds + /// e^(10^(-60)) onto 1 -- or to 0, which may be what a cancellation left of a number + /// below the working digits: arsinh(-10^60 i) is the logarithm of + /// -10^60 i + sqrt(1 - 10^120), which is -5 * 10^(-61) i, and was ln(0), -oo. + /// There the digits are doubled, and twenty more, twice at most -- what a sum leaves at the digits it is + /// made at is its residual below half of them, so a cancellation of terms of 10^60 to 10^(-61) needs + /// 242 -- and the logarithm is taken at the digits the distance they place asks for. + /// + /// + /// The residual is the downcasting's + /// (#1019), + /// and the digits a logarithm near 1 needs are what evaluation to a requested accuracy + /// asks for in v3. + /// + /// + private Complex? NearOneWithMoreDigits(Complex @base, Complex antilogarithm) + { + var context = MathS.Settings.DecimalPrecisionContext.Value; + if (context.Precision.IsZero || !context.Precision.CanFitInInt32()) + return null; + var precision = context.Precision.ToInt32Unchecked(); + var unknown = IsZeroOrOne(antilogarithm) && !IsExactArithmetic(Antilogarithm); + var distance = DistanceFromOne(@base, antilogarithm, context); + if (distance is null && !unknown) + return null; + // Evaluated again only where the two numbers are the values of the two arguments, and not of one + // element of a set or a matrix the arguments are. + if (!ReferenceEquals(Base.Evaled, @base) || !ReferenceEquals(Antilogarithm.Evaled, antilogarithm)) + return null; + var digits = DigitsFor(distance, precision, precision); + while (true) + { + var wide = Wider(context, digits); + using var _ = MathS.Settings.DecimalPrecisionContext.Set(wide); + if (Base.Evaled is not Complex wideBase || Antilogarithm.Evaled is not Complex wideAntilogarithm) + return null; + wideBase = AtTheContext(wideBase, wide); + wideAntilogarithm = AtTheContext(wideAntilogarithm, wide); + var wideDistance = DistanceFromOne(wideBase, wideAntilogarithm, wide); + var stillUnknown = unknown && IsZeroOrOne(wideAntilogarithm); + var needed = stillUnknown || wideDistance is not null ? DigitsFor(stillUnknown ? null : wideDistance, digits, precision) : digits; + if (needed > digits) + { + digits = needed; + continue; + } + var logarithm = Number.Log(wideBase, wideAntilogarithm); + var re = logarithm.RealPart.EDecimal.RoundToPrecision(context); + var im = logarithm.ImaginaryPart.EDecimal.RoundToPrecision(context); + // What the wider digits left is no residual at the working ones. + return Complex.CreateAtScales(re, im, PeterO.Numbers.EDecimal.Zero, PeterO.Numbers.EDecimal.Zero); + } + } + + private static bool IsZeroOrOne(Complex number) + => number.ImaginaryPart.EDecimal.IsZero + && (number.RealPart.EDecimal.IsZero || number.RealPart.EDecimal.CompareTo(PeterO.Numbers.EDecimal.One) == 0); + + /// + /// Whether is exact numbers in sums, differences, products, quotients and + /// whole powers, whose value is exact and is not evaluated again. + /// + private static bool IsExactArithmetic(Entity expression) + { + foreach (var node in expression.Nodes) + if (node is not (Number { IsExact: true } or Sumf or Minusf or Mulf or Divf or Powf(_, Integer))) + return false; + return true; + } + + /// + /// The larger of ||z|^2 - 1| over the base and the antilogarithm, for those of the two within + /// 10^(-precision/4) of it; where neither is. An integer is 1 exactly or far + /// from it, and is neither; a rational's distance is exact. + /// + private static (PeterO.Numbers.EDecimal Value, bool Exact)? DistanceFromOne(Complex @base, Complex antilogarithm, PeterO.Numbers.EContext context) + => (DistanceFromOne(@base, context), DistanceFromOne(antilogarithm, context)) switch + { + ({ } first, { } second) => first.Value.CompareTo(second.Value) >= 0 ? first : second, + (var first, var second) => first ?? second, + }; + + private static (PeterO.Numbers.EDecimal Value, bool Exact)? DistanceFromOne(Complex number, PeterO.Numbers.EContext context) + { + if (number is Integer) + return null; + var (re, im) = (number.RealPart.EDecimal, number.ImaginaryPart.EDecimal); + if (!re.IsFinite || !im.IsFinite) + return null; + // In doubles first, which settles all but the numbers near the unit circle. + var (reD, imD) = (re.ToDouble(), im.ToDouble()); + if (System.Math.Abs(reD * reD + imD * imD - 1) > 1e-6) + return null; + var distance = number is Rational { ERational: var ratio } + ? ratio.Multiply(ratio).Subtract(PeterO.Numbers.ERational.One).Abs().ToEDecimal(context) + : re.Multiply(re, context).Add(im.Multiply(im, context), context).Subtract(PeterO.Numbers.EDecimal.One, context).Abs(); + return distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(context.Precision.ToInt32Unchecked() / 4))) < 0 + ? (distance, number is Rational) : null; + } + + /// + /// The digits at which a logarithm of a number from the unit circle, measured + /// at digits, keeps of its own and is above half + /// of them: precision + k and 2k, for 10^(-k) the distance, and ten more, up to twenty + /// times the precision. A decimal distance at the last ten of the digits it was measured at, or none, is not known, + /// and the digits are doubled and twenty more, up to four times the precision and sixty more; past that they + /// stay . + /// + private static int DigitsFor((PeterO.Numbers.EDecimal Value, bool Exact)? measured, int measuredAt, int precision) + { + if (measured is not var (distance, exact) || distance.IsZero + || !exact && distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(measuredAt - 10))) < 0) + return 2 * measuredAt + 20 <= 4 * precision + 60 ? 2 * measuredAt + 20 : measuredAt; + var below = -(distance.Exponent.ToInt32Checked() + distance.UnsignedMantissa.GetDigitCountAsEInteger().ToInt32Checked()) + 1; + return System.Math.Min(System.Math.Max(precision + below, 2 * below + 10) + 10, 20 * precision); + } + + /// + /// at rounded up to a multiple of sixteen, one + /// instance for each: the constants are cached by the context, and a new one computed them again. + /// + private static PeterO.Numbers.EContext Wider(PeterO.Numbers.EContext context, int digits) + => widerContexts.GetOrAdd((context, (digits + 15) / 16 * 16), + static key => key.Context.WithBigPrecision(PeterO.Numbers.EInteger.FromInt32(key.Digits))); + [ConcurrentField] private static readonly System.Collections.Concurrent.ConcurrentDictionary<(PeterO.Numbers.EContext Context, int Digits), PeterO.Numbers.EContext> widerContexts = new(); + + /// + /// with each rational part written at 's digits: a + /// rational's decimal is the one it was made with, which for a literal is the working digits. + /// + private static Complex AtTheContext(Complex number, PeterO.Numbers.EContext context) + { + if (number.RealPart is not Rational && number.ImaginaryPart is not Rational || number is Integer) + return number; + static PeterO.Numbers.EDecimal Part(Real part, PeterO.Numbers.EContext context) + => part is Rational { ERational: var ratio } ? ratio.ToEDecimal(context) : part.EDecimal; + var (re, im) = (Part(number.RealPart, context), Part(number.ImaginaryPart, context)); + return im.IsZero ? Real.CreateWithoutDowncasting(re) : Complex.CreateAtScales(re, im, PeterO.Numbers.EDecimal.Zero, PeterO.Numbers.EDecimal.Zero); + } + /// /// The argument of -1, i or -i, in (-pi; pi], and /// for any other number. These are the only numbers on the unit diff --git a/Sources/Tests/UnitTests/Common/AComplexFunctionKeepsASmallPartTest.cs b/Sources/Tests/UnitTests/Common/AComplexFunctionKeepsASmallPartTest.cs index 0fed1d65a..cf48d3e7e 100644 --- a/Sources/Tests/UnitTests/Common/AComplexFunctionKeepsASmallPartTest.cs +++ b/Sources/Tests/UnitTests/Common/AComplexFunctionKeepsASmallPartTest.cs @@ -29,7 +29,9 @@ public sealed class AComplexFunctionKeepsASmallPartTest [InlineData("sinh(10^(-60) * i) / i", "1E-60", "0")] [InlineData("cos(10^(-60) * (1 + i))", "1", "-1E-120")] [InlineData("tan(10^(-60) * (1 + i))", "1E-60", "1E-60")] - [InlineData("ln(1 + 10^(-60) * i)", "0", "1E-60")] + // ln |1 + 10^(-60) i| is 5 * 10^(-121), which was 0 until the logarithm near 1 took the digits it cancels. + // https://github.com/asc-community/AngouriMath/issues/2677 + [InlineData("ln(1 + 10^(-60) * i)", "5E-121", "1E-60")] [InlineData("sin(i * 10^(-150))", "0", "1E-150")] [InlineData("exp(10^(-60) * i)", "1", "1E-60")] [InlineData("exp(pi * i)", "-1", "0")] diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs index a337616b1..8e7733446 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -668,5 +668,43 @@ public void Issue2482_ARootOfAnExactNumberIsRationalOnlyWhereItIsExact(string di Assert.True(value.EDecimal.Subtract(expected).Abs().CompareTo(expected.Multiply(EDecimal.FromString("1E-25"))) <= 0, $"{difference} is {value}"); } + + // A logarithm near 1 is taken with the digits it cancels, so a part far below the rest of the value, or below + // the working digits, is kept: ln(1 + 10^(-60)) was 0, and with it every area function past 10^50, a square + // root's imaginary part below 10^(-50) of its real part, and Ci's imaginary part, which its branch terms took for + // a residual. A logarithm of 1 that is a residual is 0 still. Each part is held to a relative 1e-10 of mpmath's + // at 1200 digits, and a part that is 0 to 0. https://github.com/asc-community/AngouriMath/issues/2677 + // These answer the downcasting's residuals, and go with it in v3: + // https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + [Theory] + [InlineData("arcoth(10^60)", "1.0e-60", "0")] + [InlineData("arcsch(10^60)", "1.0e-60", "0")] + [InlineData("arsech(10^50 * i)", "1.0e-50", "-1.57079632679489661923132169164")] + [InlineData("(1 + 10^(-50) * i)^(1/2)", "1.0", "5.0e-51")] + [InlineData("Ci(10^60 + i)", "1.2813583660587445178598677057e-60", "-6.54802065859688050757503200314e-61")] + [InlineData("Ci(10^60 - i)", "1.2813583660587445178598677057e-60", "6.54802065859688050757503200314e-61")] + [InlineData("ln(1 + 10^(-60))", "1.0e-60", "0")] + [InlineData("ln(1 + 10^(-60) * i)", "5.0e-121", "1.0e-60")] + [InlineData("ln(e^(10^(-60)))", "1.0e-60", "0")] + [InlineData("log(10, 1 + 10^(-60))", "4.34294481903251827651128918917e-61", "0")] + [InlineData("arsinh(10^(-60))", "1.0e-60", "0")] + [InlineData("artanh(10^200)", "1.0e-200", "1.57079632679489661923132169164")] + [InlineData("arsinh(-10^60 * i)", "-138.848252760202686350496719403", "-1.57079632679489661923132169164")] + [InlineData("arsech(10^200 * i)", "1.0e-200", "-1.57079632679489661923132169164")] + [InlineData("arcsch(10^200)", "1.0e-200", "0")] + [InlineData("(1 + 10^(-200) * i)^(1/3)", "1.0", "3.33333333333333333333333333333e-201")] + [InlineData("arcoth(10^20 * i)", "0", "-1.0e-20")] + [InlineData("ln(sqrt(2)^2/2)", "0", "0")] + [InlineData("ln(sin(3)^2 + cos(3)^2)", "0", "0")] + public void Issue2677_APartFarBelowTheRestOfTheValueIsKept(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-10"))) <= 0, + $"{expression} is {value}, where it is {trueReal} + {trueImaginary}i"); + } + } } } From 9784ca735789eb57a8173b88aedfdc9b17a50664 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 1 Dec 2026 23:23:08 +0000 Subject: [PATCH 2/4] Evaluate a logarithm again only for a 0 a sum left, not for a 1 (#2677) Evaluating again every logarithm of a 1 an inexact expression evaluated to cost limitcheck 15% of its time, measured by ablation: 32.0 s with the 1s, 29.2 s with the 0s alone, 27.8 s with neither. A 0 is now evaluated again only where the argument is a sum, where a cancellation can leave it. ln(e^(10^(-60))), whose argument the downcasting rounds onto 1, stays 0, and its test row and table row go. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 9 ++++----- .../Evaluation.Continuous.Arithmetics.Classes.cs | 16 ++++++++-------- .../UnitTests/Common/NumericsRegressionTest.cs | 1 - 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 0a266426a..68652356a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -154,8 +154,8 @@ being past the digits that make it an integer: `tanh(10^100 * i)` and `Ei(10^100 **Silent: wrong numbers become right ones.** `ln(1 + t)` is about `t`, and a logarithm took itself for the residual of a cancellation against 1 below half the working digits, so `ln(1 + 10^(-60))` was 0. The area functions are logarithms of `1 + 1/x` far out, so `arcoth`, `arcsch`, `artanh` and `arsech` past `10^50` lost the part that is `1/x`. A logarithm -whose argument has a modulus within `10^(-25)` of 1, or is a 0 or a 1 that an expression other than exact arithmetic -evaluated to, now evaluates its argument again with the digits the logarithm cancels, so `arsinh(-10^60 i)`, whose +whose argument has a modulus within `10^(-25)` of 1, or is a 0 that a sum other than exact arithmetic evaluated to, now +evaluates its argument again with the digits the logarithm cancels, so `arsinh(-10^60 i)`, whose argument cancels to `-5 * 10^(-61) i`, is no longer `ln(0)`. A logarithm of a 1 that is a residual is still 0. A root of a number with a decimal imaginary part other than 0 is no longer searched for among the reals, which took a phase of `10^(-60)` for 0, so `(-8 + 10^(-90) i)^(1/3)` is now the principal root `1 + 1.73 i` where it was the real `-2`. And `Ci` @@ -175,7 +175,6 @@ imaginary part. Measured on master (e3b3a34ed) and on this branch; the true valu | `arcosh(10^(-60) * i)` | `1.5707963267…i` | `1E-60 + 1.5707963267…i` | | `ln(1 + 10^(-60))` | `0` | `1E-60` | | `ln(1 + 10^(-200))` | `0` | `1E-200` | -| `ln(e^(10^(-60)))` | `0` | `1E-60` | | `ln(1 + 10^(-60) * i)` | `1E-60i` | `5E-121 + 1E-60i` | | `(1 + 10^(-50) * i)^(1/2)` | `1` | `1 + 5E-51i` | | `(1 + 10^(-200) * i)^(1/3)` | `1` | `1 + 3.3333333333…E-201i` | @@ -183,8 +182,8 @@ imaginary part. Measured on master (e3b3a34ed) and on this branch; the true valu | `Ci(10^60 + i)` | `1.2813583660…E-60` | `1.2813583660…E-60 - 6.5480206585…E-61i` | The sweep in the pull request lists the rest. A distance from 1 below the working digits is looked for at twice and four -times them, and no further: `ln(1 + 10^(-200) * i)` has its real part, `5E-401`, but `ln(e^(10^(-300)))` and -`ln(sqrt(1 + 10^(-300)))` are still 0. An exact argument's distance is known, and `ln(1 + 10^(-300))` is `1E-300`. +times them, and no further: `ln(1 + 10^(-200) * i)` has its real part, `5E-401`, but `ln(sqrt(1 + 10^(-300)))` is still 0, +and so is `ln(e^(10^(-60)))`, whose argument the downcasting rounds onto 1. An exact argument's distance is known, and `ln(1 + 10^(-300))` is `1E-300`. The new `arcsin` and `arccos` also move the last digits of moderate arguments where one part is far larger than the other: of 914 sampled evaluations of the functions above, 22 changed, all of them `arcsin`, `arccos` or `arcsec`, and each 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 e9ed2da1c..f8331489b 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 @@ -655,9 +655,8 @@ when a.Evaled is Real below && below > 0 && below < 1 /// digits, and is 0 still. https://github.com/asc-community/AngouriMath/issues/2677 /// /// - /// Where the distance from 1 is below the working digits it is not known, nor where an - /// expression that is not exact arithmetic evaluated to exactly 1 -- the downcasting rounds - /// e^(10^(-60)) onto 1 -- or to 0, which may be what a cancellation left of a number + /// Where the distance from 1 is below the working digits it is not known, nor where a sum that is + /// not exact arithmetic evaluated to 0, which may be what its cancellation left of a number /// below the working digits: arsinh(-10^60 i) is the logarithm of /// -10^60 i + sqrt(1 - 10^120), which is -5 * 10^(-61) i, and was ln(0), -oo. /// There the digits are doubled, and twenty more, twice at most -- what a sum leaves at the digits it is @@ -677,7 +676,9 @@ when a.Evaled is Real below && below > 0 && below < 1 if (context.Precision.IsZero || !context.Precision.CanFitInInt32()) return null; var precision = context.Precision.ToInt32Unchecked(); - var unknown = IsZeroOrOne(antilogarithm) && !IsExactArithmetic(Antilogarithm); + // Not a 1 that an expression evaluated to, although the downcasting rounds e^(10^(-60)) onto 1: + // evaluating each such 1 again cost limitcheck 15% of its time. + var unknown = Antilogarithm is Sumf or Minusf && IsExactlyZero(antilogarithm) && !IsExactArithmetic(Antilogarithm); var distance = DistanceFromOne(@base, antilogarithm, context); if (distance is null && !unknown) return null; @@ -695,7 +696,7 @@ when a.Evaled is Real below && below > 0 && below < 1 wideBase = AtTheContext(wideBase, wide); wideAntilogarithm = AtTheContext(wideAntilogarithm, wide); var wideDistance = DistanceFromOne(wideBase, wideAntilogarithm, wide); - var stillUnknown = unknown && IsZeroOrOne(wideAntilogarithm); + var stillUnknown = unknown && IsExactlyZero(wideAntilogarithm); var needed = stillUnknown || wideDistance is not null ? DigitsFor(stillUnknown ? null : wideDistance, digits, precision) : digits; if (needed > digits) { @@ -710,9 +711,8 @@ when a.Evaled is Real below && below > 0 && below < 1 } } - private static bool IsZeroOrOne(Complex number) - => number.ImaginaryPart.EDecimal.IsZero - && (number.RealPart.EDecimal.IsZero || number.RealPart.EDecimal.CompareTo(PeterO.Numbers.EDecimal.One) == 0); + private static bool IsExactlyZero(Complex number) + => number.ImaginaryPart.EDecimal.IsZero && number.RealPart.EDecimal.IsZero; /// /// Whether is exact numbers in sums, differences, products, quotients and diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs index 8e7733446..313ee6cea 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -685,7 +685,6 @@ public void Issue2482_ARootOfAnExactNumberIsRationalOnlyWhereItIsExact(string di [InlineData("Ci(10^60 - i)", "1.2813583660587445178598677057e-60", "6.54802065859688050757503200314e-61")] [InlineData("ln(1 + 10^(-60))", "1.0e-60", "0")] [InlineData("ln(1 + 10^(-60) * i)", "5.0e-121", "1.0e-60")] - [InlineData("ln(e^(10^(-60)))", "1.0e-60", "0")] [InlineData("log(10, 1 + 10^(-60))", "4.34294481903251827651128918917e-61", "0")] [InlineData("arsinh(10^(-60))", "1.0e-60", "0")] [InlineData("artanh(10^200)", "1.0e-200", "1.57079632679489661923132169164")] From 27da25622ffaceda3473793be7e31e8f981f45a8 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Wed, 2 Dec 2026 05:56:40 +0000 Subject: [PATCH 3/4] Read a written decimal's distance from 1 to the working digits only (#2677) The solver writes a trigonometric root as ln(a + b i) / i, with a and b decimals to the working digits and |a + b i| = 1 to within their rounding, about 10^(-101). Evaluated again with more digits, a written decimal is the same decimal, so that distance read as placed, and the logarithm returned it: the real roots of cos(3x) = sin(2x) and sin(2x) = sin(3x) came back with imaginary parts near 10^(-101), and trigcheck dropped them under scaling. Where either argument has a decimal in it, a distance is known to the working digits alone, and a 0 a sum left is not evaluated again. Co-Authored-By: Claude Opus 5.5 --- ...aluation.Continuous.Arithmetics.Classes.cs | 41 +++++++++++++++---- .../Common/NumericsRegressionTest.cs | 18 ++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) 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 f8331489b..2dd001f0f 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 @@ -678,15 +678,25 @@ when a.Evaled is Real below && below > 0 && below < 1 var precision = context.Precision.ToInt32Unchecked(); // Not a 1 that an expression evaluated to, although the downcasting rounds e^(10^(-60)) onto 1: // evaluating each such 1 again cost limitcheck 15% of its time. - var unknown = Antilogarithm is Sumf or Minusf && IsExactlyZero(antilogarithm) && !IsExactArithmetic(Antilogarithm); + // A decimal written in either argument -- a root the solver wrote as ln(a + b i) with a and b to the + // working digits -- carries no more digits than those, and evaluated again it is the same decimal: + // what it places below them is its own rounding, |a + b i| - 1 of about 10^(-101) where the root is + // real, and the logarithm is a residual there as before. var distance = DistanceFromOne(@base, antilogarithm, context); + var zeroOfASum = Antilogarithm is Sumf or Minusf && IsExactlyZero(antilogarithm); + if (distance is null && !zeroOfASum) + return null; + var inexact = HasADecimal(Base) || HasADecimal(Antilogarithm); + var unknown = zeroOfASum && !inexact && !IsExactArithmetic(Antilogarithm); if (distance is null && !unknown) return null; // Evaluated again only where the two numbers are the values of the two arguments, and not of one // element of a set or a matrix the arguments are. if (!ReferenceEquals(Base.Evaled, @base) || !ReferenceEquals(Antilogarithm.Evaled, antilogarithm)) return null; - var digits = DigitsFor(distance, precision, precision); + var digits = DigitsFor(distance, precision, precision, inexact); + if (digits <= precision) + return null; while (true) { var wide = Wider(context, digits); @@ -697,7 +707,7 @@ when a.Evaled is Real below && below > 0 && below < 1 wideAntilogarithm = AtTheContext(wideAntilogarithm, wide); var wideDistance = DistanceFromOne(wideBase, wideAntilogarithm, wide); var stillUnknown = unknown && IsExactlyZero(wideAntilogarithm); - var needed = stillUnknown || wideDistance is not null ? DigitsFor(stillUnknown ? null : wideDistance, digits, precision) : digits; + var needed = stillUnknown || wideDistance is not null ? DigitsFor(stillUnknown ? null : wideDistance, digits, precision, inexact) : digits; if (needed > digits) { digits = needed; @@ -711,6 +721,15 @@ when a.Evaled is Real below && below > 0 && below < 1 } } + /// Whether has a number in it that is not exact. + private static bool HasADecimal(Entity expression) + { + foreach (var node in expression.Nodes) + if (node is Number { IsExact: false }) + return true; + return false; + } + private static bool IsExactlyZero(Complex number) => number.ImaginaryPart.EDecimal.IsZero && number.RealPart.EDecimal.IsZero; @@ -760,15 +779,19 @@ private static (PeterO.Numbers.EDecimal Value, bool Exact)? DistanceFromOne(Comp /// The digits at which a logarithm of a number from the unit circle, measured /// at digits, keeps of its own and is above half /// of them: precision + k and 2k, for 10^(-k) the distance, and ten more, up to twenty - /// times the precision. A decimal distance at the last ten of the digits it was measured at, or none, is not known, - /// and the digits are doubled and twenty more, up to four times the precision and sixty more; past that they - /// stay . + /// times the precision. A decimal distance at the last ten of the digits it was measured at, or of the working digits where + /// says an argument has a decimal written in it, or none, is not known, + /// and the digits are doubled and twenty more, up to four times the precision and sixty more, and not at all + /// for a written decimal, whose digits evaluating again does not add to; past that they stay + /// . /// - private static int DigitsFor((PeterO.Numbers.EDecimal Value, bool Exact)? measured, int measuredAt, int precision) + private static int DigitsFor((PeterO.Numbers.EDecimal Value, bool Exact)? measured, int measuredAt, int precision, bool inexact) { + // A written decimal is known to the working digits alone, whatever the digits it is measured at. + var knownTo = inexact ? precision : measuredAt; if (measured is not var (distance, exact) || distance.IsZero - || !exact && distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(measuredAt - 10))) < 0) - return 2 * measuredAt + 20 <= 4 * precision + 60 ? 2 * measuredAt + 20 : measuredAt; + || !exact && distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(knownTo - 10))) < 0) + return inexact ? measuredAt : 2 * measuredAt + 20 <= 4 * precision + 60 ? 2 * measuredAt + 20 : measuredAt; var below = -(distance.Exponent.ToInt32Checked() + distance.UnsignedMantissa.GetDigitCountAsEInteger().ToInt32Checked()) + 1; return System.Math.Min(System.Math.Max(precision + below, 2 * below + 10) + 10, 20 * precision); } diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs index 313ee6cea..8fac09700 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -705,5 +705,23 @@ public void Issue2677_APartFarBelowTheRestOfTheValueIsKept(string expression, st $"{expression} is {value}, where it is {trueReal} + {trueImaginary}i"); } } + + // But a decimal the solver wrote, a + b i to the working digits with |a + b i| = 1, carries no digits past them, + // and the 10^(-101) its modulus is off 1 by is its own rounding: the logarithm of it is a residual, and the + // root ln(a + b i) / i is real. Taken for a value, the roots of these two came back off the real line. + // https://github.com/asc-community/AngouriMath/issues/2677 + [Theory] + [InlineData("cos(3*x) = sin(2*x)")] + [InlineData("sin(2*x) = sin(3*x)")] + public void Issue2677_TheLogarithmOfAWrittenDecimalOnTheUnitCircleIsImaginary(string equation) + { + var logarithms = System.Linq.Enumerable.ToList(System.Linq.Enumerable.OfType(equation.ToEntity().Solve("x").Nodes)); + Assert.NotEmpty(logarithms); + foreach (var logarithm in logarithms) + { + var value = Assert.IsAssignableFrom(logarithm.Evaled); + Assert.True(value.RealPart.EDecimal.IsZero, $"{logarithm} is {value}"); + } + } } } From 5ab14bbcd1fb8a42949c92bde5ec00d1f0783b17 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Wed, 2 Dec 2026 07:52:40 +0000 Subject: [PATCH 4/4] Leave a modulus on the unit circle to its parts' rounding (#2677) symlimitcheck was 16% slower, and the logarithm near 1 was the whole of it: 6178 logarithms, 404 of them evaluated again at 224 and 464 digits, 4.2 s. They were arguments with a modulus exactly 1, -7/10 + sqrt(51)/10 i above all, and arcosh-like ones of 3/5 + 4/5 i, which more digits cannot move off 1. The distance was taken rounded at each step, so the square of sqrt(51)/10 came out 0.51 exactly and the modulus read as 1 below the working digits, not known. The distance is now taken exactly and rounded after, so such a modulus is 1 within its parts' rounding, about 10^(-100), and that band, ten digits either side of the last known one, keeps the working digits. A number with two rational parts has its distance exactly, and 0 is on the circle. Below the band, 0 among it, the digits are doubled as before, which is what arcsch(10^200) and arsech(10^200 i) need. In symlimitcheck the method now takes 43 ms and evaluates nothing again. Co-Authored-By: Claude Opus 5.5 --- ...aluation.Continuous.Arithmetics.Classes.cs | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) 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 2dd001f0f..e477bb9b7 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 @@ -768,30 +768,44 @@ private static (PeterO.Numbers.EDecimal Value, bool Exact)? DistanceFromOne(Comp var (reD, imD) = (re.ToDouble(), im.ToDouble()); if (System.Math.Abs(reD * reD + imD * imD - 1) > 1e-6) return null; - var distance = number is Rational { ERational: var ratio } - ? ratio.Multiply(ratio).Subtract(PeterO.Numbers.ERational.One).Abs().ToEDecimal(context) - : re.Multiply(re, context).Add(im.Multiply(im, context), context).Subtract(PeterO.Numbers.EDecimal.One, context).Abs(); + // A number whose parts are both rational has its distance exactly: 3/5 + 4/5 i is on the circle. + var exact = number.RealPart is Rational && number.ImaginaryPart is Rational; + var distance = exact + ? ((Rational)number.RealPart).ERational.Multiply(((Rational)number.RealPart).ERational) + .Add(((Rational)number.ImaginaryPart).ERational.Multiply(((Rational)number.ImaginaryPart).ERational)) + .Subtract(PeterO.Numbers.ERational.One).Abs().ToEDecimal(context) + // Exactly, and rounded after: rounded at each step, the square of a hundred digits of sqrt(51)/10 is + // 0.51 exactly, and -7/10 + sqrt(51)/10 i was exactly 1 rather than 1 within its parts' rounding. + : re.Multiply(re).Add(im.Multiply(im)).Subtract(PeterO.Numbers.EDecimal.One).Abs().RoundToPrecision(context); return distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(context.Precision.ToInt32Unchecked() / 4))) < 0 - ? (distance, number is Rational) : null; + ? (distance, exact) : null; } /// /// The digits at which a logarithm of a number from the unit circle, measured /// at digits, keeps of its own and is above half /// of them: precision + k and 2k, for 10^(-k) the distance, and ten more, up to twenty - /// times the precision. A decimal distance at the last ten of the digits it was measured at, or of the working digits where - /// says an argument has a decimal written in it, or none, is not known, - /// and the digits are doubled and twenty more, up to four times the precision and sixty more, and not at all - /// for a written decimal, whose digits evaluating again does not add to; past that they stay - /// . + /// times the precision. An exact distance of 0 is on the circle, and a decimal one within ten digits of the + /// last it is known to -- those it was measured at, or the working digits where + /// says an argument has a decimal written in it -- is its parts' rounding: either keeps + /// . A decimal distance further below, 0 among them, or none, is not known, and + /// the digits are doubled and twenty more, up to four times the precision and sixty more, and not at all for + /// a written decimal, whose digits evaluating again does not add to. /// private static int DigitsFor((PeterO.Numbers.EDecimal Value, bool Exact)? measured, int measuredAt, int precision, bool inexact) { // A written decimal is known to the working digits alone, whatever the digits it is measured at. var knownTo = inexact ? precision : measuredAt; - if (measured is not var (distance, exact) || distance.IsZero - || !exact && distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(knownTo - 10))) < 0) - return inexact ? measuredAt : 2 * measuredAt + 20 <= 4 * precision + 60 ? 2 * measuredAt + 20 : measuredAt; + var doubled = inexact ? measuredAt : 2 * measuredAt + 20 <= 4 * precision + 60 ? 2 * measuredAt + 20 : measuredAt; + if (measured is { } onTheCircle && onTheCircle.Exact && onTheCircle.Value.IsZero) + return measuredAt; + if (measured is not var (distance, exact) || distance.IsZero) + return doubled; + if (!exact && distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(knownTo - 10))) < 0) + // Within ten digits either side of the last it was measured to, it is what rounding the parts + // leaves of a modulus that is 1, as -7/10 + sqrt(51)/10 i has, and more digits leave it at their + // own last: so it stays. Further below it is no rounding of the parts, and is not known. + return distance.CompareTo(PeterO.Numbers.EDecimal.Create(1, -(knownTo + 10))) < 0 ? doubled : measuredAt; var below = -(distance.Exponent.ToInt32Checked() + distance.UnsignedMantissa.GetDigitCountAsEInteger().ToInt32Checked()) + 1; return System.Math.Min(System.Math.Max(precision + below, 2 * below + 10) + 10, 20 * precision); }