diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md
index d26ab2afa..dbfcaafd1 100644
--- a/BREAKING-CHANGES.md
+++ b/BREAKING-CHANGES.md
@@ -189,6 +189,46 @@ The new `arcsin` and `arccos` also move the last digits of moderate arguments wh
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.
+### arsinh of a large negative number, a square root past 10^50, and Ci off the real line keep their digits
+
+**Silent: wrong numbers become right ones.** `arsinh(x)` is `ln(x + sqrt(x^2 + 1))`, and for a large negative `x` the
+two terms cancel to about `-1/(2x)`, losing twice the digits of `x`; from `-10^116` they cancelled to 0 at every
+precision the logarithm tries, and the answer was `-oo`. A logarithm of `a + sqrt(b)`, or of a difference of the two,
+whose terms cancel is now taken of the same number written `(a^2 - b)/(a - sqrt(b))`, where `b` is written `a^2 + c` or
+both are exact. `arcsch` reaches the same logarithm. Separately, a square root of an exact number past `10^50` that is
+not a rational was the root near 1 of the number scaled by a power of ten, which the downcasting rounds onto 1 within
+half the working digits, scaled back: `sqrt((10^50 + 1/3)^2)` was `10^50`. And `Ci` and `Si` past their series added
+`i pi` from each of `Ei(z)` and `Ei(-z)` and `pi/2` from each branch term, which for `Ci` off the real line add up to
+0, beside an imaginary part near `1/|z|` of which the sum at the working digits kept a few or none; the constants are
+now added up first. Where a complex number's real part is a decimal far enough out, `10^100` and past, that its cosine
+leaves a part of `Ci` or `Si` below that part's residual, the value is not known, as `cos(10^100 + i)` has none, and
+is `NaN` rather than 0. Measured on master (abd100a33) and on this branch; the true values are mpmath's at 700 digits
+([#2683](https://github.com/asc-community/AngouriMath/issues/2683)).
+
+| `.Evaled` | Was (master) | Now |
+|---|---|---|
+| `arsinh(-10^30 - 1/3)` | `-69.770699970381315829956975761989436129440544…`, right to 40 digits | `-69.770699970381315829956975761989436129441878…` |
+| `arsinh(-10^50 - 1/3)` | `-1.0986122886… + 3.1415926535…i`, which is `ln(-1/3)` | `-115.82240183026222951…` |
+| `arsinh(-10^115 + 1/2)` | `-0.6931471805…`, which is `ln(1/2)` | `-265.49043287487519897…` |
+| `arsinh(-10^116)` | `-oo` | `-267.79301796786924465…` |
+| `arsinh(-10^200)` | `-oo` | `-461.21016577936908211…` |
+| `arsinh(-10^120 * i)` | `-oo` | `-277.00335833984542739… - 1.5707963267…i` |
+| `arsinh(-10^200 * i)` | `-oo` | `-461.21016577936908211… - 1.5707963267…i` |
+| `arcsch(10^(-200) * i)` | `-oo` | `-461.21016577936908211… - 1.5707963267…i` |
+| `sqrt((10^50 + 1/3)^2)` | `100000000000000000000000000000000000000000000000000` | `100000000000000000000000000000000000000000000000000.3333333333…` |
+| `sqrt(10^60 + 1)` | `1000000000000000000000000000000` | `1000000000000000000000000000000.0000000000000000000000000000005` |
+| `Ci(10^66 + i)` | `-9.3193344373…E-67` | `-9.3193344373…E-67 + 9.3666728029…E-67i` |
+| `Ci(10^80 + i)` | `-8.7322816510…E-82` | `-8.7322816510…E-82 - 1.1733179410…E-80i` |
+| `Ci(10^90 + i)` | `-1.5350414397…E-90` | `-1.5350414397…E-90 + 1.1980441014…E-91i` |
+| `Ci(10^100 + i)` | `0` | `NaN` |
+| `Si(10^100 + i)` | `1.5707963267…`, with no imaginary part | `NaN` |
+
+A square root past `10^50` that the working digits do not place as an integer is now a decimal where it was an integer:
+`sqrt(10^100 + 1)` prints `100000000000000000000000000000000000000000000000000.0000…` where it printed the integer. Of
+668 sampled values of `arsinh`, `arcsch`, `Ci` and `Si`, master had 151 wrong past a relative `1e-90` and 136 not finite,
+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.
+
### 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/Core/Entity/Continuous/Number/ExponentialIntegrals.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs
index 8ea420af5..af8105d35 100644
--- a/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs
+++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs
@@ -114,14 +114,51 @@ public static Complex Li(Complex z)
///
/// says that is exactly the number asked about.
private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) Ei(EDecimal re, EDecimal im, EContext context, int extraDigits, bool exactIm = false)
+ {
+ var (valueRe, valueIm, reScale, imScale, turns) = EiApartFromItsConstant(re, im, context, extraDigits, exactIm);
+ if (turns == 0)
+ return (valueRe, valueIm, reScale, imScale);
+ var work = Working(context, extraDigits + 10);
+ var (pi, piScale) = HalfTurns(2 * turns, context, work);
+ return (valueRe, valueIm.Add(pi, work), reScale, imScale.Add(piScale, work));
+ }
+
+ ///
+ /// times pi/2 at 's digits, and the scale it is a
+ /// residual against at 's: a constant cancels nothing, so it is a residual only
+ /// against its digits 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
+ ///
+ private static (EDecimal Value, EDecimal Scale) HalfTurns(int halfTurns, EContext context, EContext work)
+ {
+ var value = EDecimal.PI(work).Multiply(halfTurns, work).Divide(2, work);
+ return (value, value.Abs().ScaleByPowerOfTen(context.Precision.ToInt32Checked() - work.Precision.ToInt32Checked()));
+ }
+
+ ///
+ /// less the constant i pi sgn(Im z) its
+ /// asymptotic series adds, and how many times i pi that is: 0 where it is summed as its series, which
+ /// has no such constant apart.
+ ///
+ ///
+ /// Kept apart so that the four trigonometric integrals add their constants up before adding them to
+ /// anything: Ci(10^80 + i) is i pi from Ei(z), -i pi from Ei(-z), and i pi/2 and -i pi/2 from the branch
+ /// terms, beside an imaginary part of about -1.2 * 10^(-80), which their sum at the working digits and ten
+ /// more read as the residual of their cancellation. https://github.com/asc-community/AngouriMath/issues/2683
+ ///
+ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale, int Turns) EiApartFromItsConstant(EDecimal re, EDecimal im, EContext context, int extraDigits, bool exactIm)
{
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))
{
- var asymptoticWork = Working(context, extraDigits + 10);
- return EiByTheAsymptoticSeries(re, im, asymptoticWork, exactIm,
- asymptoticWork.Precision.ToInt32Checked() - context.Precision.ToInt32Checked());
+ var (asymptoticRe, asymptoticIm, asymptoticReScale, asymptoticImScale) = EiByTheAsymptoticSeries(re, im, Working(context, extraDigits + 10), exactIm);
+ return (asymptoticRe, asymptoticIm, asymptoticReScale, asymptoticImScale, im.IsZero ? 0 : im.IsNegative ? -1 : 1);
}
// The digits the terms cancel off the positive axis, and ten over.
var cancelled = (int)Math.Ceiling((modulus - re.ToDouble()) / Math.Log(10));
@@ -130,7 +167,7 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) Ei
// The sum is taken with the digits it cancels and more, so what it leaves is a residual only against
// its terms less those digits: Ei(-130), about -2.7 * 10^(-59), out of terms near 10^54.
var beyond = context.Precision.ToInt32Checked() - work.Precision.ToInt32Checked();
- return (value.Re, value.Im, scale.Re.ScaleByPowerOfTen(beyond), scale.Im.ScaleByPowerOfTen(beyond));
+ return (value.Re, value.Im, scale.Re.ScaleByPowerOfTen(beyond), scale.Im.ScaleByPowerOfTen(beyond), 0);
}
///
@@ -167,10 +204,10 @@ 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.
+ /// e^z/z sum_k k!/z^k, cut at its smallest term, and for each part the sizes of the products added
+ /// to make it: Ei less its constant i pi sgn(Im z).
///
- private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) EiByTheAsymptoticSeries(EDecimal re, EDecimal im, EContext work, bool exactIm, int guard)
+ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) EiByTheAsymptoticSeries(EDecimal re, EDecimal im, EContext work, bool exactIm)
{
var epsilon = EDecimal.Create(EInteger.One, EInteger.FromInt32(-work.Precision.ToInt32Checked()));
// 1/z = (re - i im)/|z|^2
@@ -206,17 +243,6 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) Ei
var factorImScale = growth.Multiply(sin.Abs().Multiply(absRe, work).Add(cos.Abs().Multiply(absIm, work), work).Add(trigonometric, work), work).Divide(squaredModulus, work);
var reScale = factorReScale.Multiply(sum.Re.Abs(), work).Add(factorImScale.Multiply(sum.Im.Abs(), work), work);
var imScale = factorReScale.Multiply(sum.Im.Abs(), work).Add(factorImScale.Multiply(sum.Re.Abs(), work), work);
- if (!im.IsZero)
- {
- var pi = EDecimal.PI(work);
- valueIm = valueIm.Add(im.IsNegative ? pi.Negate() : 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 9beeecd95..b2cdd779c 100644
--- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
+++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
@@ -441,13 +441,33 @@ private static bool CanHaveARealRoot(Complex @base, EInteger rootPower) =>
Real factor = Integer.Create(EInteger.FromInt32(10).Pow(shift.Abs()));
Complex power10 = Integer.Create(EInteger.FromInt32(10).Pow(shift.Abs().Multiply(power.EInteger)));
var scaled = shift.Sign < 0 ? @base * power10 : @base / power10;
- return FindGoodRootNearOne(scaled, power) is { } root ? (shift.Sign < 0 ? root / factor : root * factor) : null;
+ // Only a rational root is scaled back. Any other is the base's own: near 1 it is the integer it
+ // is within half the working digits of, which scaled back is the tolerance times the scale, and
+ // sqrt((10^50 + 1/3)^2) was 10^50. https://github.com/asc-community/AngouriMath/issues/2683
+ var root = FindGoodRootNearOne(scaled, power, rationalOnly: true);
+ if (root is not null)
+ return shift.Sign < 0 ? root / factor : root * factor;
+ return @base is Real { EDecimal: var realBase } && realBase.IsFinite
+ ? RealRoot(realBase, power.EInteger)
+ : FindGoodRootNearOne(@base, power, rationalOnly: false);
}
}
- return FindGoodRootNearOne(@base, power);
+ return FindGoodRootNearOne(@base, power, rationalOnly: false);
}
- private static Real? FindGoodRootNearOne(Complex @base, Integer power)
+ ///
+ /// The real -th root of a real base, positive for a positive base and negative
+ /// for a negative one, which lets through only for an odd root.
+ ///
+ private static Real RealRoot(EDecimal @base, EInteger rootPower)
+ {
+ var context = MathS.Settings.DecimalPrecisionContext.Value;
+ var modulus = PowerOfReals(@base.Abs(), EDecimal.One.Divide(EDecimal.FromEInteger(rootPower), InternalAMExtensions.WithGuardDigits(context, 8)), context);
+ // No cancellation made it, so it is never a residual: sqrt(1/10^186) is 10^(-93), not 0.
+ return Real.CreateAtScale(@base.IsNegative ? modulus.Negate() : modulus, EDecimal.Zero);
+ }
+
+ private static Real? FindGoodRootNearOne(Complex @base, Integer power, bool rationalOnly)
{
Real? positive = null, real = null;
foreach (var root in GetAllRoots(@base, power.EInteger))
@@ -465,6 +485,8 @@ private static bool CanHaveARealRoot(Complex @base, EInteger rootPower) =>
// https://github.com/asc-community/AngouriMath/issues/2482
if (rounded is Rational rational && IsZeroAtTheWorkingPrecision(Pow(rational, power) - @base))
return rational;
+ if (rationalOnly)
+ continue;
// The root as GetAllRoots made it, at the working precision.
switch (root)
{
diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs
index 04af179d1..7b57d7d65 100644
--- a/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs
+++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/TrigonometricIntegrals.cs
@@ -108,7 +108,17 @@ private static Complex TrigonometricIntegral(Complex z, bool hyperbolic, bool od
// (https://github.com/asc-community/AngouriMath/issues/2074), and the imaginary parts of
// Si(1 + 10^(-60) i) and Chi(1 + 10^(-60) i) are about 10^(-60).
// https://github.com/asc-community/AngouriMath/issues/1944
- return Complex.CreateAtScales(valueRe.RoundToPrecision(context), valueIm.RoundToPrecision(context), reScale, imScale);
+ (valueRe, valueIm) = (valueRe.RoundToPrecision(context), valueIm.RoundToPrecision(context));
+ // Off the line the four oscillate along, a part there is the oscillation alone, and where a decimal to
+ // oscillate along leaves it below the residual of its own cosine and sine, its value is not known: it is
+ // not 0. Ci(10^100 + i) was 0, and Si(10^100 + i) had no imaginary part; cos(10^100 + i) has no value either.
+ // https://github.com/asc-community/AngouriMath/issues/2683
+ if (!alongIsExact && !(hyperbolic ? re : im).IsZero && (IsAResidual(valueRe, reScale) || IsAResidual(valueIm, imScale)))
+ return Real.NaN;
+ return Complex.CreateAtScales(valueRe, valueIm, reScale, imScale);
+
+ static bool IsAResidual(EDecimal value, EDecimal scale)
+ => !scale.IsZero && value.Abs().LessThan(MathS.Settings.DowncastingTolerance.Multiply(scale.Abs()));
}
///
@@ -180,26 +190,48 @@ private static ((EDecimal Re, EDecimal Im) Sum, (EDecimal Re, EDecimal Im) Scale
///
private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) ByTheExponentialIntegral(EDecimal re, EDecimal im, bool hyperbolic, bool odd, EContext context, bool alongIsExact)
{
- if (!hyperbolic)
+ var work = Working(context, 10);
+ // The constants, multiples of pi/2, are added up before they are added to anything, and only what is
+ // left of them is added: see EiApartFromItsConstant. https://github.com/asc-community/AngouriMath/issues/2683
+ EDecimal valueRe, valueIm, reScale, imScale;
+ int halfTurns;
+ var constantIsReal = false;
+ if (hyperbolic)
+ (valueRe, valueIm, reScale, imScale, halfTurns) = HyperbolicApartFromItsConstant(re, im, odd, context, alongIsExact);
+ else
{
// Si(z) = -i Shi(i z), and Ci(z) = Chi(i z) + ln z - ln(i z): i z = -im + i re.
- var (valueRe, valueIm, reScale, imScale) = ByTheExponentialIntegral(im.Negate(), re, hyperbolic: true, odd, context, alongIsExact);
+ var (shiftedRe, shiftedIm, shiftedReScale, shiftedImScale, shiftedHalfTurns) = HyperbolicApartFromItsConstant(im.Negate(), re, odd, context, alongIsExact);
if (odd)
- // -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 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);
- // 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())));
+ {
+ // -i (a + i b) = b - i a, and -i times i pi/2 is pi/2.
+ (valueRe, valueIm, reScale, imScale, halfTurns) = (shiftedIm, shiftedRe.Negate(), shiftedImScale, shiftedReScale, shiftedHalfTurns);
+ constantIsReal = true;
+ }
+ else
+ {
+ // ln z - ln(i z) is -i pi/2 where arg z <= pi/2, and 3 i pi/2 past it.
+ var pastAQuarterTurn = re.IsNegative && !im.IsNegative;
+ (valueRe, valueIm, reScale, imScale, halfTurns) = (shiftedRe, shiftedIm, shiftedReScale, shiftedImScale, shiftedHalfTurns + (pastAQuarterTurn ? 3 : -1));
+ }
}
+ if (halfTurns == 0)
+ return (valueRe, valueIm, reScale, imScale);
+ var (constant, constantScale) = HalfTurns(halfTurns, context, work);
+ return constantIsReal
+ ? (valueRe.Add(constant, work), valueIm, reScale.Add(constantScale, work), imScale)
+ : (valueRe, valueIm.Add(constant, work), reScale, imScale.Add(constantScale, work));
+ }
+
+ ///
+ /// Shi or Chi past the series' reach, less their constant, and how many times i pi/2 that is: the
+ /// constants of Ei(z) and Ei(-z) and the branch terms of (ln z - ln(1/z))/2 in each.
+ ///
+ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale, int HalfTurns) HyperbolicApartFromItsConstant(EDecimal re, EDecimal im, bool odd, EContext context, bool alongIsExact)
+ {
var work = Working(context, 10);
- var (plusRe, plusIm, plusReScale, plusImScale) = Ei(re, im, context, extraDigits: 10, alongIsExact);
- var (minusRe, minusIm, minusReScale, minusImScale) = Ei(re.Negate(), im.Negate(), context, extraDigits: 10, alongIsExact);
+ var (plusRe, plusIm, plusReScale, plusImScale, plusTurns) = EiApartFromItsConstant(re, im, context, extraDigits: 10, alongIsExact);
+ var (minusRe, minusIm, minusReScale, minusImScale, minusTurns) = EiApartFromItsConstant(re.Negate(), im.Negate(), context, extraDigits: 10, alongIsExact);
var half = EDecimal.FromString("0.5");
var (sumRe, sumIm) = odd
? (plusRe.Subtract(minusRe, work).Multiply(half, work), plusIm.Subtract(minusIm, work).Multiply(half, work))
@@ -207,23 +239,17 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) By
// Half of Ei(z) and Ei(-z) each, so a residual against half the scales of both.
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();
+ // Half of i pi from each Ei.
+ var halfTurns = odd ? plusTurns - minusTurns : plusTurns + minusTurns;
// 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.ScaleByPowerOfTen(beyond), work);
+ var sign = im.IsNegative ? -1 : 1;
+ halfTurns += odd ? -sign : sign;
}
else if (!odd && re.IsNegative)
- {
- sumIm = sumIm.Add(EDecimal.PI(work), work);
- sumImScale = sumImScale.Add(EDecimal.PI(work).ScaleByPowerOfTen(beyond), work);
- }
- return (sumRe, sumIm, sumReScale, sumImScale);
+ halfTurns += 2;
+ return (sumRe, sumIm, sumReScale, sumImScale, halfTurns);
}
}
}
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 58750e3fb..7f9325db2 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) is var logarithm && MayBeNearOne(logarithm)
+ (Complex n1, Complex n2) when !isExact => 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
@@ -639,6 +639,67 @@ when a.Evaled is Real below && below > 0 && below < 1
},
(@this, a, b) => ((Logf)@this).New(a, b), isExact);
+ ///
+ /// The antilogarithm s a + t sqrt(b), with s and t each 1 or -1, as
+ /// (a^2 - b) / (s a - t sqrt(b)) where its two terms cancel to below a hundredth of a and
+ /// a^2 - b is known without a cancellation of its own: b is written a^2 + c, or both
+ /// are exact arithmetic. anywhere else.
+ ///
+ ///
+ /// arsinh(x) is ln(x + sqrt(x^2 + 1)), and for a large negative x the two terms cancel
+ /// to about -1/(2x), losing twice the digits of x: arsinh(-10^30 - 1/3) was right to 40
+ /// of a hundred digits, and from -10^116 the sum was 0 at every precision the logarithm asks for, and
+ /// arsinh(-10^200) and arsinh(-10^200 i) were -oo. The conjugate is the same number, so the
+ /// principal branch is kept.
+ /// https://github.com/asc-community/AngouriMath/issues/2683
+ ///
+ private Complex? WithoutARootsCancellation(Complex antilogarithm)
+ {
+ var (a, root, s, t) = Antilogarithm switch
+ {
+ Sumf(var term, Powf(_, Rational half) radical) when IsAHalf(half) => (term, radical, 1, 1),
+ Sumf(Powf(_, Rational half) radical, var term) when IsAHalf(half) => (term, radical, 1, 1),
+ Minusf(var term, Powf(_, Rational half) radical) when IsAHalf(half) => (term, radical, 1, -1),
+ Minusf(Powf(_, Rational half) radical, var term) when IsAHalf(half) => (term, radical, -1, 1),
+ _ => (null!, null!, 0, 0),
+ };
+ if (s == 0 || !ReferenceEquals(Antilogarithm.Evaled, antilogarithm) || a.Evaled is not Complex aValue)
+ return null;
+ var b = root.Base;
+ // At least two digits cancelled, so that s a - t sqrt(b) is within a few per cent of 2 s a.
+ var size = Magnitude(aValue);
+ if (size == long.MinValue || Magnitude(antilogarithm) > size - 3)
+ return null;
+ Complex? difference = b switch
+ {
+ Sumf(var square, var rest) when IsTheSquareOf(square, a) => rest.Evaled is Complex c ? -c : null,
+ Sumf(var rest, var square) when IsTheSquareOf(square, a) => rest.Evaled is Complex c ? -c : null,
+ Minusf(var square, var rest) when IsTheSquareOf(square, a) => rest.Evaled as Complex,
+ _ when IsExactArithmetic(a) && IsExactArithmetic(b) && b.Evaled is Complex bValue => aValue * aValue - bValue,
+ _ => null,
+ };
+ if (difference is null || root.Evaled is not Complex rootValue)
+ return null;
+ // 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;
+
+ 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)
+ {
+ 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));
+ }
+ }
+
///
/// Whether a logarithm, as the working digits give it, may be one of an argument near 1, or of a 0 a
/// cancellation left, or one to a base near 1: its real part is 0, below 10^(-precision/4), -oo, or
diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
index 8fac09700..a7946a849 100644
--- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
+++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
@@ -723,5 +723,54 @@ public void Issue2677_TheLogarithmOfAWrittenDecimalOnTheUnitCircleIsImaginary(st
Assert.True(value.RealPart.EDecimal.IsZero, $"{logarithm} is {value}");
}
}
+
+ // ln(a + sqrt(a^2 + 1)) cancels for a large negative a, losing twice its digits, and from -10^116 was ln(0); the
+ // conjugate, -1/(a - sqrt(a^2 + 1)), cancels nothing. A square root of an exact number past 10^50 was the
+ // integer its root near 1 rounded onto, scaled back. Ci and Si past the series add i pi from Ei(z) and Ei(-z) and
+ // pi/2 from each branch term, which add up to 0 for Ci off the real line, beside a part near 10^(-80) they left
+ // only 36 digits of, or none. Each part is held to a relative 1e-90 of mpmath's at 700 digits, and a part that
+ // is 0 to 0. https://github.com/asc-community/AngouriMath/issues/2683
+ [Theory]
+ [InlineData("arsinh(-10^30 - 1/3)", "-6.9770699970381315829956975761989436129441878126556777868453850566298349690068046900463557257307824427924491792e+1", "0")]
+ [InlineData("arsinh(-10^50 - 1/3)", "-1.1582240183026222951031680485567638694813057456579890738912040839120535743917065206073905691480966693799316846e+2", "0")]
+ [InlineData("arsinh(-10^115 + 1/2)", "-2.6549043287487519897148624941016006044220217132666914749795338862076424373486522994274554191230022299686829665e+2", "0")]
+ [InlineData("arsinh(-10^116)", "-2.6779301796786924465550424086484442464980327281529792047398671652173181634454258242298153911738982129521026443e+2", "0")]
+ [InlineData("arsinh(-10^300)", "-6.9146867507877365051481466852676743884840594672299214806411905029976517652517543878640502485387590819013233669e+2", "0")]
+ [InlineData("arsinh(-10^29 * i)", "-6.7468114877387270145938984307304738588507443304594671559087164137552999302612916642449782274594769339459067218e+1", "-1.5707963267948966192313216916397514420985846996875529104874722961539082031431044993140174126710585339910740433")]
+ [InlineData("arsinh(-10^200 * i)", "-4.6121016577936908211301552305833101808829579786011485046078626020300791555744019076280530434491607835593555829e+2", "-1.5707963267948966192313216916397514420985846996875529104874722961539082031431044993140174126710585339910740433")]
+ [InlineData("arcsch(10^(-200) * i)", "-4.6121016577936908211301552305833101808829579786011485046078626020300791555744019076280530434491607835593555829e+2", "-1.5707963267948966192313216916397514420985846996875529104874722961539082031431044993140174126710585339910740433")]
+ [InlineData("arsinh(-20)", "-3.689503868988905640821653570961093329678760832868086366520509251274995424465766662121747017302471761593370327", "0")]
+ [InlineData("arsinh(-1/2)", "-4.8121182505960344749775891342436842313518433438566051966101816884016386760822177441200942912272347499723183996e-1", "0")]
+ [InlineData("sqrt((10^50 + 1/3)^2)", "1.0000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333e+50", "0")]
+ [InlineData("sqrt(10^60 + 1)", "1.0000000000000000000000000000000000000000000000000000000000005e+30", "0")]
+ [InlineData("Ci(10^66 + i)", "-9.31933443734638814204908161922033406722621263311724091765581211253639516274593834896421686655323366472684748e-67", "9.3666728029509277702699262821523226224071102893559105130283814333693070455199687350923045331568323537713895405e-67")]
+ [InlineData("Ci(10^80 + i)", "-8.7322816510785566719207212091936368258450137517711860408160051819784499940346458162167835738169831867734559164e-82", "-1.1733179410565691770506999754066925953031783567667153875760953071110328899316974453093416302184379011974914409e-80")]
+ [InlineData("Ci(10^83 + i)", "1.1024303545076221502747746185246464070970353943684927477760139815976729549348899640941781337756983809687572842e-84", "1.1721981437112079066604546604866629606407811585917748604922688780784361183874692670783801268739700270727060478e-83")]
+ [InlineData("Ci(-10^80 - i)", "-8.7322816510785566719207212091936368258450137517711860408160051819784499940346458162167835738169831867734559164e-82", "-3.1415926535897932384626433832795028841971693993751058209749445923078164062862090103612142359078088384891478406")]
+ [InlineData("Si(10^80 + i)", "1.570796326794896619231321691639751442098584699687552910487472296153908203143104514720095989838057409975619943", "-6.6504546736211864024417690623055627204591225858675248868728586732303618394659041248107596834686526488423054513e-82")]
+ [InlineData("Si(10^80 - i)", "1.570796326794896619231321691639751442098584699687552910487472296153908203143104514720095989838057409975619943", "6.6504546736211864024417690623055627204591225858675248868728586732303618394659041248107596834686526488423054513e-82")]
+ public void Issue2683_ACancellationOfARootOrOfAConstantKeepsItsDigits(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-90"))) <= 0,
+ $"{expression} is {value}, where it is {trueReal} + {trueImaginary}i");
+ }
+ }
+
+ // A complex number's part past 10^50 is a decimal, whose cosine and sine from 10^100 leave Ci's parts below their
+ // residual, as cos(10^100 + i) has no value: such a part is not known, and is not 0. Ci(10^100 + i) was 0 and
+ // Si(10^100 + i) had no imaginary part. https://github.com/asc-community/AngouriMath/issues/2683
+ [Theory]
+ [InlineData("Ci(10^100 + i)")]
+ [InlineData("Si(10^100 + i)")]
+ [InlineData("Ci(10^110 + i)")]
+ [InlineData("Ci(10^200 + i)")]
+ public void Issue2683_ATrigonometricIntegralOfADecimalPastItsDigitsIsNaN(string expression)
+ {
+ Assert.Equal(MathS.NaN, expression.ToEntity().Evaled);
+ }
}
}