Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,51 @@ public static Complex Li(Complex z)
/// </summary>
/// <remarks><paramref name="exactIm"/> says that <paramref name="im"/> is exactly the number asked about.</remarks>
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));
}

/// <summary>
/// <paramref name="halfTurns"/> times <c>pi/2</c> at <paramref name="work"/>'s digits, and the scale it is a
/// residual against at <paramref name="context"/>'s: a constant cancels nothing, so it is a residual only
/// against its digits past the context's.
/// </summary>
/// <remarks>
/// 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
/// </remarks>
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()));
}

/// <summary>
/// <see cref="Ei(EDecimal, EDecimal, EContext, int, bool)"/> less the constant <c>i pi sgn(Im z)</c> its
/// asymptotic series adds, and how many times <c>i pi</c> that is: 0 where it is summed as its series, which
/// has no such constant apart.
/// </summary>
/// <remarks>
/// 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
/// </remarks>
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));
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -167,10 +204,10 @@ private static ((EDecimal Re, EDecimal Im) Value, (EDecimal Re, EDecimal Im) Sca
}

/// <summary>
/// <c>e^z/z sum_k k!/z^k + i pi sgn(Im z)</c>, cut at its smallest term, and for each part the sizes of the
/// products added to make it.
/// <c>e^z/z sum_k k!/z^k</c>, cut at its smallest term, and for each part the sizes of the products added
/// to make it: Ei less its constant <c>i pi sgn(Im z)</c>.
/// </summary>
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
Expand Down Expand Up @@ -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);
}

Expand Down
28 changes: 25 additions & 3 deletions Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// <summary>
/// The real <paramref name="rootPower"/>-th root of a real base, positive for a positive base and negative
/// for a negative one, which <see cref="CanHaveARealRoot"/> lets through only for an odd root.
/// </summary>
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))
Expand All @@ -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)
{
Expand Down
Loading
Loading