Skip to content
Open
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
36 changes: 36 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,42 @@ 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 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`
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(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(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
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -166,7 +170,7 @@ private static ((EDecimal Re, EDecimal Im) Value, (EDecimal Re, EDecimal Im) Sca
/// <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.
/// </summary>
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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Loading
Loading