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
50 changes: 50 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,56 @@ mpmath's at 4500 digits.
| `Ci(exp(300))` | `0` | `NaN` | below `1e-130`, sign unknown |
| `cos(10^150/3)` | `-0.44819…` | `0.80881903093981837173…` | `0.80881903093981837173…` |

### A complex value past 10^50 keeps every part of it

**Silent: wrong numbers become right ones.** The complex paths of the exponential, the trigonometric functions and the
exponential and trigonometric integrals took a part for a rounding residual when it was below half the working digits of
the argument's size. A sine or a cosine is at most 1, so past `10^50` every one was a residual: `e^(10^60 i)` kept its real
part alone, `tanh(10^60 i)` came back with a real part of -2.2 where it has none, and `Ci(10^50)` was 0. A part is now a
residual against `|t|` for an argument `t` up to 1, against `sqrt|t|` past it for a decimal, half the digits its reduction
by `2 pi` leaves, and against 1 for an exact rational, which is reduced exactly; so `Ci` and `Si` of an exact `10^500`,
which were `NaN`, have values. `arcsin` and `arccos` of a non-real argument took their real part from
`(|z + 1| - |z - 1|)/2`, two moduli of the argument's size, so that past `10^110` it had no digits and `arcsin(10^200)`
was 0, and dropped their imaginary part against the size of the argument. They are now written as Hull, Fairgrieve and
Tang write them, with nothing cancelling, at any size up to the exponents' end; past `10^1000` they were `+-oo i` with a
real part that was `pi/2` whatever the direction, and an infinite argument now goes off along its direction. And a root
of an exact non-real number was taken for a real one when its imaginary part was below the tolerance:
`sqrt(10^50 + i)` was `10^25`. `Complex.CreatePolar` measures its residuals the same way. Measured on master
(4674e6656) and on this branch; the true values are mpmath's at 700 to 2000 digits, `arcsin` of a real past 1 taken
from below the axis as the library takes it ([#2675](https://github.com/asc-community/AngouriMath/issues/2675)).

| `.Evaled` | Was (master) | Now |
|---|---|---|
| `arcsin(10^100)` | `1.5707963267…` | `1.5707963267… - 230.95165647…i` |
| `arcsin(10^200)` | `0` | `1.5707963267… - 461.21016577…i` |
| `arcsin(10^100 * i)` | `0` | `230.95165647…i` |
| `arccos(10^500)` | `1.5707963267…` | `1151.98569367…i` |
| `arccos(10^100 * i)` | `1.5707963267…` | `1.5707963267… - 230.95165647…i` |
| `arcsin(10^1500)` | `1.5707963267… - oo i` | `1.5707963267… - 3454.57078667…i` |
| `arcsin(10^1500 * i)` | `1.5707963267… + oo i` | `3454.57078667…i` |
| `arccos(-10^1500)` | `+oo i` | `3.1415926535… + 3454.57078667…i` |
| `arcsin(-oo)` | `1.5707963267… - oo i` | `-1.5707963267… - oo i` |
| `arcsin(+oo * i)` | `1.5707963267… + oo i` | `+oo i` |
| `e^(10^60 * i)` | `-0.5571829482…` | `-0.5571829482… + 0.8303897652…i` |
| `sin(10^60 + i)` | `1.2813583660…` | `1.2813583660… - 0.6548020658…i` |
| `tanh(10^60 * i)` | `-2.2211011728… - 1.4903359261…i` | `-1.4903359261…i` |
| `tanh(10^50 * i)` | `-1.65663… + 1.2870997095…i` | `1.2870997095…i` |
| `coth(10^50 * i)` | `-0.376417… - 0.292454…i` | `-0.7769405840…i` |
| `Ci(10^50)` | `0` | `-7.8967249342…E-51` |
| `Ci(10^100)` | `0` | `-3.7237612366…E-101` |
| `Ci(10^500)` | `NaN` | `3.1102693090…E-501` |
| `Si(10^500)` | `NaN` | `1.5707963267…` |
| `Ei(10^50 * i)` | `3.1415926535…i` | `-7.8967249342…E-51 + 3.1415926535…i` |
| `Ei(-10^60 * i)` | `-3.1415926535…i` | `8.3038976521…E-61 - 3.1415926535…i` |
| `sqrt(10^50 + i)` | `10^25` | `10^25 + 5E-26i` |

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`.

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.

### 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 @@ -180,8 +180,10 @@ public void Deconstruct(out Real realPart, out Real imaginaryPart) =>
public static Complex CreatePolar(EDecimal magnitude, EDecimal phase)
{
var context = MathS.Settings.DecimalPrecisionContext;
// The residual of a cosine or a sine is the magnitude's: cos(pi/2) leaves 1e-100 of it.
var scale = magnitude.Abs();
// The residual of a cosine or a sine is its argument's times the magnitude: cos(pi/2) leaves 1e-100
// of it, and the root of 1 + 10^(-50) i, 1 + 5 10^(-51) i, lost its imaginary part at the magnitude
// alone, so that sqrt(10^50 + i) was real. https://github.com/asc-community/AngouriMath/issues/2675
var scale = magnitude.Abs().Multiply(PeriodicResidualScale(phase, exact: false, context), context);
return CreateAtScales(magnitude.Multiply(phase.Cos(context), context), magnitude.Multiply(phase.Sin(context), context), scale, scale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public static Complex Ei(Complex z)
if (digitsOfTheRest < -(context.Precision.ToInt32Checked() + 2))
return Complex.Create(EDecimal.Zero, im.IsNegative ? EDecimal.PI(context).Negate() : EDecimal.PI(context));
}
var ei = Ei(re, im, context, extraDigits: 0);
// An exact imaginary part is reduced exactly: Ei(10^100 i) was NaN for want of the digits of cos(10^100).
// https://github.com/asc-community/AngouriMath/issues/2675
var (exactIm, imIsExact) = PeriodicArgument(z.ImaginaryPart, im, context);
var ei = Ei(re, exactIm, context, extraDigits: 0, imIsExact);
// A part is a residual only against what was added to make it, not against 1: Ei(-130) is about
// -2.7 * 10^(-59). https://github.com/asc-community/AngouriMath/issues/1944
return Complex.CreateAtScales(ei.Re.RoundToPrecision(context), ei.Im.RoundToPrecision(context), ei.ReScale, ei.ImScale);
Expand Down Expand Up @@ -109,12 +112,13 @@ public static Complex Li(Complex z)
/// <paramref name="extraDigits"/> more, before any rounding back, and for each part the scale it is a
/// residual against at <paramref name="context"/>'s precision.
/// </summary>
private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) Ei(EDecimal re, EDecimal im, EContext context, int extraDigits)
/// <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 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));
return EiByTheAsymptoticSeries(re, im, Working(context, extraDigits + 10), exactIm);
// 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 @@ -162,7 +166,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)
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 All @@ -184,14 +188,16 @@ private static (EDecimal Re, EDecimal Im, EDecimal ReScale, EDecimal ImScale) Ei
}
// e^z/z = e^re (cos im + i sin im) (re - i im)/|z|^2
var growth = re.Exponential(work);
var (cos, sin) = (im.Cos(work), im.Sin(work));
var (sin, cos) = exactIm ? InternalAMExtensions.SinAndCosOfAnExactArgument(im, work) : InternalAMExtensions.SinAndCos(im, work);
var exponential = (growth.Multiply(cos, work), growth.Multiply(sin, work));
var factor = Multiply(exponential, reciprocal, work);
var (valueRe, valueIm) = Multiply(factor, sum, work);
// Each part of e^z/z is a sum of two products, and cos im and sin im are known to |im| times the
// working digits, as on the real line; the value's parts add those parts times the sum's.
// Each part of e^z/z is a sum of two products, and cos im and sin im are residuals against
// PeriodicResidualScale, as on the real line; the value's parts add those parts times the sum's.
// Against |im| itself, Ci(10^50), about -7.9e-51, was 0.
// https://github.com/asc-community/AngouriMath/issues/2675
var (absRe, absIm) = (re.Abs(), im.Abs());
var trigonometric = absIm.Multiply(absRe.Add(absIm, work), work);
var trigonometric = PeriodicResidualScale(im, exactIm, work).Multiply(absRe.Add(absIm, work), work);
var factorReScale = growth.Multiply(cos.Abs().Multiply(absRe, work).Add(sin.Abs().Multiply(absIm, work), work).Add(trigonometric, work), work).Divide(squaredModulus, work);
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);
Expand Down
Loading
Loading