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
41 changes: 41 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,47 @@ Of 8400 values of `cos`, `sin`, `tan`, `sec`, `csc`, `cot`, `cosh`, `sinh`, `tan
16 imaginary parts of `tanh` are now right, and of the rows right on master only the last digit of
`sinh(10^4 ± pi/4 i)` moved.

### tanh, coth, sech and csch off the real line, and a reciprocal near 1e-13, keep their small parts and digits

**Silent: wrong numbers become right ones.** `tanh` and `coth` are spelled `(e^(2 z) ∓ 1)/(e^(2 z) ± 1)`, and `sech`
and `csch` as reciprocals of `(e^z ± e^(-z))/2`. Evaluated as written, a part small beside the two powers cancelled in
the quotient or the sum and was dropped, and `e^(2 z) - 1` read its imaginary part against the 1, so `tanh(-100 + i)`
had half its imaginary part. Off the real line each is now taken as the trigonometric function of `i z`, `tanh z` as
`-i tan(i z)`, whose parts cancel nothing but a residual of a multiple of `pi/2`, and `e^u - 1` reads each part
against what it is a sum of. `tan` and `cot` off the real line now read a sine or a cosine that is a residual of the
real part as 0, as `sin` and `cos` do, and `tan`'s denominator is `cos^2 x + sinh^2 y` where it was
`cos 2x + cosh 2y`, a residual itself near a pole. A real quotient below 0.1 was divided to a hundred decimal places
and kept down to 86 digits, so `sec(1/2 - 30 i)`, the reciprocal of a cosine near `5e12`, kept 88; it keeps 99.
`tanh(100 + i)`, which has the imaginary part `2.5E-87`, below `1e-50` of its modulus, was 1 and now keeps it, as
`tanh(-100 + i)` does: the part is no residual, and `tanh` is odd. Measured on master (0ce20c5a8) and on this
branch; the true values are mpmath's at 450 and 1000 digits ([#2701](https://github.com/asc-community/AngouriMath/issues/2701)).

| `.Evaled` | Was (master) | Now |
|---|---|---|
| `tanh(10^(-60) + i)` | `1.557407…i` | `3.425518…E-60 + 1.557407…i` |
| `coth(10^(-60) + i)` | `-0.642092…i` | `1.412282…E-60 - 0.642092…i` |
| `tanh(-100 + i)` | `-1 + 1.258373…E-87i` | `-1 + 2.516747…E-87i` |
| `tanh(-100 + 1/3 * i)` | `-1 + 8.557598…E-88i` | `-1 + 1.711519…E-87i` |
| `tanh(100 + i)` | `1` | `1 + 2.516747…E-87i` |
| `sech(10^(-60) + i)` | `1.850815…` | `1.850815… - 2.882474…E-60i` |
| `tanh(10^(-60) * i)` | `1E-120 + 1E-60i` | `1E-60i` |
| `tanh(10^(-60) + pi/2 * i)` | `NaN` | `1E+60` |
| `tan(pi/2 + 10^(-60) * i)` | `-oo + +ooi` | `1E+60i` |
| `cot(pi + 10^(-30) * i)` | `1.785190…E-41 - 1E+30i` | `-1E+30i` |
| `sec(1/2 - 30 * i)` | 88 digits | 99 digits |

Of 3798 values of `tanh`, `coth`, `sech`, `csch`, `tan`, `cot`, `sec`, `csc` and `1/z` at `a + b i`, for `a` in
`±10^(-k)` (`k` in 20, 40, 60, 80, 99, 120), `±1/3`, `±1`, `±10`, `±100` and `±1000` and `b` in `±1`, `±1/3`,
`±pi/4` and `±10^(-k)`, and at 26 ordinary and large arguments, master had 3040 right to a relative `1e-95` in each
part, 39 wrong, 289 with a small part dropped, 314 with a part right to fewer digits (96 of them to fewer than 85),
100 with a part within `1e-50` of an integer, which the downcasting makes that integer, and the other part right to
fewer than 95 digits, and 16 `NaN`. This branch has 3716 right, none wrong, 10 `NaN` (four poles, and `sech` and
`csch` past the exponents, as before) and 72 with a part made an integer and the other right to 80 digits, 16 of
which, `sech` near 0, had that part dropped. No part right on master is further than `2.1e-99` from
its value now. The last digit of the real part of `tan(3 ± 2 i)`, which was pinned, is now the correctly rounded
one, `…418949` where it was `…418947`; the true value is `…4189487`. Of the 8400 values of #2700's sweep, master had 12 wrong and 12 `NaN`, `tanh` of `-100 + b i` and of
`2 10^5 + b i`, and this branch has every one right.

### 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
109 changes: 74 additions & 35 deletions Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,28 @@ internal static EDecimal CtxDivide(EDecimal a, EDecimal b)
if (b.IsZero)
return EDecimal.NaN;
var context = MathS.Settings.DecimalPrecisionContext.Value;
// A quotient surely below 0.1 to as many more places as it has zeros after the point, and one more for
// the estimate, which is the size of each side from its exponent and its mantissa's bits, a third of a
// digit either way, then rounded to the digits: dividing to places, and to digits only where too few came
// of it, a quotient near 1e-13 kept 88 digits, and so did sec(1/2 - 30 i), the reciprocal of a cosine
// near 5e12. https://github.com/asc-community/AngouriMath/issues/2701
if (!context.Precision.IsZero && Log10OfTheSizeOf(a) is { } ofA && Log10OfTheSizeOf(b) is { } ofB
&& ofA - ofB < -1.5 && ofA - ofB > -1000)
return a.DivideToExponent(b, -context.Precision.Add(EInteger.FromInt32(1 + (int)System.Math.Ceiling(ofB - ofA))))
.RoundToPrecision(context);
var quotient = a.DivideToExponent(b, -context.Precision);
// 3.3 bits a digit, and five digits to spare; an unlimited context keeps the division to places.
// Fewer bits than a mantissa of all but one of the digits has, log2(10) = 3.32 a digit; an unlimited
// context keeps the division to places.
if (!context.Precision.IsZero && quotient.IsFinite && !a.IsZero
&& quotient.UnsignedMantissa.GetUnsignedBitLengthAsInt64() < (context.Precision.ToInt64Checked() - 5) * 3)
&& quotient.UnsignedMantissa.GetUnsignedBitLengthAsInt64() < (long)((context.Precision.ToInt64Checked() - 1) * log2OfTen))
return a.Divide(b, context);
return quotient;
}
private const double log2OfTen = 3.3219280948873623;
private static double? Log10OfTheSizeOf(EDecimal value)
=> value.IsFinite && !value.IsZero && value.Exponent.CanFitInInt32()
? value.Exponent.ToInt32Unchecked() + value.UnsignedMantissa.GetUnsignedBitLengthAsInt64() * 0.30102999566398120
: null;
internal static EDecimal CtxMod(EDecimal a, EDecimal b)
=> a.Remainder(b, MathS.Settings.DecimalPrecisionContext);
internal static EDecimal CtxPow(EDecimal a, EDecimal b)
Expand Down Expand Up @@ -1290,42 +1305,48 @@ public static Complex Tan(Complex num)
if (num is Real { EDecimal: var real } asReal)
{
var (sinOfReal, cosOfReal) = SinAndCosOfAReal(asReal, real, context);
return Real.CreateAtScale(cosOfReal.IsZero ? EDecimal.NaN : sinOfReal.Divide(cosOfReal, context), real);
// A cosine that is a residual of the argument is 0, as cos reads it: tan of pi/2 to the working digits,
// which tanh(pi/2 i) is, was 1.1E+101. https://github.com/asc-community/AngouriMath/issues/2701
return Real.CreateAtScale(IsAResidualOfItsArgument(cosOfReal, PeriodicResidualScale(asReal, real, context), MathS.Settings.DowncastingEnabled)
? EDecimal.NaN : sinOfReal.Divide(cosOfReal, context), real);
}
// Off the real line a part past the reach is the infinity it was, as for Exp.
num = WithinTheSpecialFunctionsReach(num);
// From https://source.dot.net/#System.Runtime.Numerics/System/Numerics/Complex.cs,cf15f2e5cc49cef1

// tan z = sin z / cos z, but to avoid unnecessary repeated trig computations, use
// tan z = (sin(2x) + i sinh(2y)) / (cos(2x) + cosh(2y))
// (see Abramowitz & Stegun 4.3.57 or derive by hand), and compute trig functions here.

// This approach does not work for |y| > ~355, because sinh(2y) and cosh(2y) overflow,
// even though their ratio does not. In that case, divide through by cosh to get:
// tan z = (sin(2x) / cosh(2y) + i \tanh(2y)) / (1 + cos(2x) / cosh(2y))
// which correctly computes the (tiny) real part and the (normal-sized) imaginary part.

// The real part is sin(2 re) over the rest, a residual of 2 re as on the real line; the imaginary
// part cancels nothing. tan(1e-60 (1 + i)) is 1e-60 (1 + i), and was 0.
Real twiceRe = num.RealPart * Integer.Create(2);
var x2 = twiceRe.EDecimal;
var y2 = num.ImaginaryPart.EDecimal.Multiply(2, context);
var (sinh, cosh) = SinhAndCosh(y2, context);
var (sinOfX2, cosOfX2) = SinAndCosOfAReal(twiceRe, x2, context);
var x2Scale = PeriodicResidualScale(twiceRe, x2, context);
EDecimal ReScale(EDecimal divisor) => divisor.IsZero ? EDecimal.One : x2Scale.Divide(divisor.Abs(), context);
if (num.ImaginaryPart.EDecimal.Abs().LessThanOrEquals(4))
// tan z = (sin x cos x + i sinh y cosh y) / (cos^2 x + sinh^2 y), and past |y| = 4, divided through by cosh^2 y,
// (sin x cos x sech^2 y + i tanh y) / (cos^2 x sech^2 y + tanh^2 y), which past the exponents is i sgn y.
// Nothing in it cancels but sin x or cos x, which is 0 where it is a residual of x, as for sin and cos, and
// tan(1e-60 (1 + i)) is 1e-60 (1 + i). Its denominator was cos 2x + cosh 2y, of which cos 2x is -1 at a pole,
// so near one it was the residual of that sum: tan(pi/2 + 10^(-60) i), which is 10^60 i, was -oo + oo i,
// and tanh(10^(-60) + pi/2 i) with it. https://github.com/asc-community/AngouriMath/issues/2701
var (x, y) = (num.RealPart.EDecimal, num.ImaginaryPart.EDecimal);
// Within |y| <= 4 with ten digits more, rounded once, as the parts were to the last digit before: each
// of the products and the quotient rounds. Past it the exponentials take the context's exponents, past
// which cosh y is infinite, and the parts are as near 1 and 0 as the last digit tells.
var small = y.Abs().LessThanOrEquals(4);
var work = small ? Working(context, 10) : context.Value;
var (sinOfX, cosOfX) = SinAndCosOfAReal(num.RealPart, x, work);
var xScale = PeriodicResidualScale(num.RealPart, x, context);
var readsResiduals = MathS.Settings.DowncastingEnabled.Value;
if (IsAResidualOfItsArgument(sinOfX, xScale, readsResiduals))
sinOfX = EDecimal.Zero;
if (IsAResidualOfItsArgument(cosOfX, xScale, readsResiduals))
cosOfX = EDecimal.Zero;
var sinCos = sinOfX.Multiply(cosOfX, work);
var cosSquared = cosOfX.Multiply(cosOfX, work);
var (sinh, cosh) = SinhAndCosh(y, work);
if (small)
{
var D = cosOfX2.Add(cosh, context);
return Complex.CreateAtScales(sinOfX2.Divide(D, context), sinh.Divide(D, context), ReScale(D), EDecimal.Zero);
var D = cosSquared.Add(sinh.Multiply(sinh, work), work);
return Complex.CreateAtScales(sinCos.Divide(D, work).RoundToPrecision(context),
sinh.Multiply(cosh, work).Divide(D, work).RoundToPrecision(context), EDecimal.Zero, EDecimal.Zero);
}
else
{
var D = cosOfX2.Divide(cosh, context).Increment();
return Complex.CreateAtScales(
sinOfX2.Divide(cosh, context).Divide(D, context),
y2.Tanh(context).Divide(D, context),
ReScale(cosh.Multiply(D, context)), EDecimal.Zero);
var tanh = y.Tanh(context);
var sech = cosh.IsFinite ? EDecimal.One.Divide(cosh, context) : EDecimal.Zero;
var sechSquared = sech.Multiply(sech, context);
var D = cosSquared.Multiply(sechSquared, context).Add(tanh.Multiply(tanh, context), context);
return Complex.CreateAtScales(sinCos.Multiply(sechSquared, context).Divide(D, context), tanh.Divide(D, context), EDecimal.Zero, EDecimal.Zero);
}
}

Expand Down Expand Up @@ -1359,6 +1380,15 @@ public static Complex Cotan(Complex num)
if (y.IsZero)
return Real.CreateAtScale(sin, x).IsZero ? Integer.One / Tan(num)
: Real.CreateAtScale(cos.Divide(sin, context), x.Abs().Divide(sin.Abs(), context));
// Off the real line a sine or a cosine that is a residual of x is 0, as for sin and cos: the real part of
// cot(pi + 10^(-30) i), which is -10^30 i, was pi's residual over tanh^2(10^(-30)), 1.8E-41, above the
// tolerance of a number near 0 and below that of its scale. https://github.com/asc-community/AngouriMath/issues/2701
var xScale = PeriodicResidualScale(num.RealPart, x, context);
var readsResiduals = MathS.Settings.DowncastingEnabled.Value;
if (IsAResidualOfItsArgument(sin, xScale, readsResiduals))
sin = EDecimal.Zero;
if (IsAResidualOfItsArgument(cos, xScale, readsResiduals))
cos = EDecimal.Zero;
var (sinh, cosh) = SinhAndCosh(y, context);
// Past the context's exponents cosh y is infinite, tanh y its sign and sech y 0: cot(1 - 3000 i) is i.
var (tanh, sech) = cosh.IsFinite
Expand All @@ -1371,7 +1401,7 @@ public static Complex Cotan(Complex num)
var re = sin.Multiply(cos, context).Multiply(sechSquared, context).Divide(denominator, context);
var im = tanh.Negate().Divide(denominator, context);
// sin x cos x is a residual of x through whichever of the two is small.
var reScale = PeriodicResidualScale(num.RealPart, x, context).Multiply(sin.Abs().Add(cos.Abs(), context), context).Multiply(sechSquared, context).Divide(denominator, context);
var reScale = xScale.Multiply(sin.Abs().Add(cos.Abs(), context), context).Multiply(sechSquared, context).Divide(denominator, context);
return Complex.CreateAtScales(re, im, reScale, EDecimal.Zero);
}

Expand Down Expand Up @@ -1485,9 +1515,18 @@ internal static Complex ExpDifference(Complex v, Complex d)
}
// Each sum rounded: taken exactly, 1 added to e^(10^7) was an integer of four million digits, and
// tanh(10^7) did not return. https://github.com/asc-community/AngouriMath/issues/2563
EDecimal Size(Complex z) => z.RealPart.EDecimal.Abs().Add(z.ImaginaryPart.EDecimal.Abs(), work);
var scale = Size(lower).Multiply(Size(power).Add(EDecimal.One, work), work).ScaleByPowerOfTen(-extra);
return Complex.CreateAtScales(product.RealPart.EDecimal.RoundToPrecision(context), product.ImaginaryPart.EDecimal.RoundToPrecision(context), scale, scale);
// Each part against what it is a sum of: only Re(e^d) - 1 cancels, at the size of |Re(e^d)| + 1, and each
// part of the product takes it through one part of e^v and Im(e^d) through the other. Against one scale
// for both, Im(e^(-200 + 2 i) - 1), 1.3E-87, was read against the 1 and dropped, and tanh(-100 + i), whose
// quotient takes it once from each side, had half its imaginary part.
// https://github.com/asc-community/AngouriMath/issues/2701
var (lowerRe, lowerIm) = (lower.RealPart.EDecimal.Abs(), lower.ImaginaryPart.EDecimal.Abs());
var cancelled = power.RealPart.EDecimal.Abs().Add(EDecimal.One, work);
var powerIm = power.ImaginaryPart.EDecimal.Abs();
EDecimal Scale(EDecimal withTheOne, EDecimal withTheSine)
=> withTheOne.Multiply(cancelled, work).Add(withTheSine.Multiply(powerIm, work), work).ScaleByPowerOfTen(-extra);
return Complex.CreateAtScales(product.RealPart.EDecimal.RoundToPrecision(context), product.ImaginaryPart.EDecimal.RoundToPrecision(context),
Scale(lowerRe, lowerIm), Scale(lowerIm, lowerRe));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public partial record Sumf
/// <inheritdoc/>
protected override Entity InnerSimplify(bool isExact)
{
if (!isExact && Functions.HyperbolicSpellings.Cosh(Augend, Addend) is { } cosh)
return cosh;
SimplifyALongLeftSpineFirst(this, isExact);
return ExpandOnTwoArguments(Augend, Addend,
(augend, addend) => (augend, addend) switch
Expand Down Expand Up @@ -198,6 +200,8 @@ partial record Minusf
/// </summary>
private static Entity? DifferenceOfExponentials(Entity minuend, Entity subtrahend)
{
if (Functions.HyperbolicSpellings.Sinh(minuend, subtrahend) is { } sinh)
return sinh;
if (ExponentOfE(minuend) is not { } u || ExponentOfE(subtrahend) is not { } v)
return null;
if ((u - v).Evaled is not Complex d || v.Evaled is not Complex exponent || !d.IsFinite || !exponent.IsFinite)
Expand Down Expand Up @@ -253,6 +257,7 @@ public partial record Divf

/// <inheritdoc/>
protected override Entity InnerSimplify(bool isExact) =>
!isExact && Functions.HyperbolicSpellings.TanhOrCoth(Dividend, Divisor) is { } quotient ? quotient :
ExpandOnTwoArguments(Dividend, Divisor,
(a, b) => (a, b) switch
{
Expand Down
Loading
Loading