diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md
index 9a55f9536..17efa936d 100644
--- a/BREAKING-CHANGES.md
+++ b/BREAKING-CHANGES.md
@@ -324,6 +324,36 @@ eighteen ordinary arguments, master had 368 `NaN`; this branch has 300 of them r
infinity of each part's sign, and none that was right on master changed. The 28 left `NaN` are the hyperbolic
functions and `exp` of `pi/4 ± 10^100 i` and `pi/4 ± 10^300 i`, whose imaginary part is a decimal there, as before.
+### A part that is 0 for an exact argument is 0 however large the other part
+
+**Silent: a wrong number becomes 0.** A part of `cos(x + i y)`, `sin(x + i y)` or `e^(y + i x)` is the sine or the
+cosine of `x` times `cosh y`, `sinh y` or `e^y`. Where that sine or cosine is 0 for the exact `x`, `cos(pi/2)` among them,
+the hundred digits leave about `1e-100` of it, and the product was read as 0 only while it was below the downcasting
+tolerance itself, `1e-50`. From `|y|` near 116 a real part that is 0 was that residual times `cosh y`. The imaginary
+part was already read against its scale at any size. Now the factor is read as on the real line, against the size of `x`,
+and a part whose factor is a residual is 0. `sinh`, `cosh`, `tanh`, `sec` and `csc`, which are built from these, follow.
+A factor that is small but not 0 keeps its part: `cos(1/10^40 + 1000 i)` and `exp(1000 + 10^(-80) i)` are unchanged. A
+factor within the tolerance of 0 relative to `x` is now 0 at every `y`, as it is on the real line and was at small
+`y` and past the exponents: `cos(pi/2 + 10^(-60) + 100 i)` had a real part of `-1.34E-17` and now has none, as
+`cos(pi/2 + 10^(-60))` is `0`. Measured on master (28bec0a4d) and on this branch; the true values are mpmath's at
+450 and 1000 digits ([#2700](https://github.com/asc-community/AngouriMath/issues/2700)).
+
+| `.Evaled` | Was (master) | Now |
+|---|---|---|
+| `cos(pi/2 + 1000 * i)` | `-8.79…E+332 - 9.85…E+433i` | `-9.85…E+433i` |
+| `cos(pi/2 + 10^5 * i)` | `-1.25…E+43328 - 1.40…E+43429i` | `-1.40…E+43429i` |
+| `sin(pi + 1000 * i)` | `-1.76…E+333 - 9.85…E+433i` | `-9.85…E+433i` |
+| `exp(1000 + pi/2 * i)` | `-1.76…E+333 + 1.97…E+434i` | `1.97…E+434i` |
+| `sinh(1000 + pi/2 * i)` | `-8.79…E+332 + 9.85…E+433i` | `9.85…E+433i` |
+| `tanh(10^5 + pi/4 * i)` | `1 + 3.49…E-108i` | `1 + 2.54…E-86859i` |
+| `tanh(2 * 10^5 + pi/2 * i)` | `NaN` | `1` |
+
+Of 8400 values of `cos`, `sin`, `tan`, `sec`, `csc`, `cot`, `cosh`, `sinh`, `tanh` and `exp` of `a ± i y` and
+`± y + a i`, for `a` in `k pi/2` (`k` from -4 to 4), `±10^(-40)`, `±10^(-80)`, `±1`, `±1/3`, `±pi/3` and `±pi/4` and
+`y` from 10 to `2 10^5`, master had 416 with a part that is 0 written as a number; this branch has none, 8 `NaN` and
+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.
+
### 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/Operators.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
index 4ede9985f..83215f129 100644
--- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
+++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
@@ -615,7 +615,13 @@ public static Complex Sqrt(Complex num)
}
/// exp(x) = e^x
- public static Complex Exp(Complex x)
+ public static Complex Exp(Complex x) => Exp(x, MathS.Settings.DowncastingEnabled);
+
+ ///
+ /// e^x, with a sine or a cosine of x's imaginary part that is a residual of it read as 0 where
+ /// , whatever the downcasting in force.
+ ///
+ private static Complex Exp(Complex x, bool readsResiduals)
{
// Off the real line a part past the reach is the infinity it was: the cosine and the sine of an
// imaginary part past 10^50 are below the residual its size allows, and e^(2 10^5000 i) kept its real
@@ -630,13 +636,13 @@ public static Complex Exp(Complex x)
return Real.CreateAtScale(expReal, EDecimal.Zero);
// From https://source.dot.net/#System.Runtime.Numerics/System/Numerics/Complex.cs,7dc9c2ee4f99814a
var (sinOfImaginary, cosOfImaginary) = SinAndCosOfAReal(x.ImaginaryPart, imaginary, context);
- var cosImaginary = expReal.Multiply(cosOfImaginary, context);
- var sinImaginary = expReal.Multiply(sinOfImaginary, context);
// The residual of a cosine or a sine is its argument's, as on the real line, times the modulus:
// e^(i pi) leaves 1e-100 i of pi, and e^(1e-60 i) is 1 + 1e-60 i, whose sine was 0 at the
// modulus alone.
- var scale = expReal.Multiply(PeriodicResidualScale(x.ImaginaryPart, imaginary, context), context);
- return Complex.CreateAtScales(cosImaginary, sinImaginary, scale, scale);
+ var imaginaryScale = PeriodicResidualScale(x.ImaginaryPart, imaginary, context);
+ var scale = expReal.Multiply(imaginaryScale, context);
+ return Complex.CreateAtScales(TimesAHyperbolic(cosOfImaginary, imaginaryScale, readsResiduals, expReal, context),
+ TimesAHyperbolic(sinOfImaginary, imaginaryScale, readsResiduals, expReal, context), scale, scale);
}
/// e.g. Pow(2, 5) = 32
@@ -1106,7 +1112,7 @@ private static EDecimal TimesAHyperbolicPastTheExponents(EDecimal factor, EDecim
{
if (factor.IsNaN() || y.IsNaN())
return EDecimal.NaN;
- if (factor.IsZero || MathS.Settings.DowncastingEnabled && factor.Abs().LessThan(MathS.Settings.DowncastingTolerance.Multiply(factorScale.Abs())))
+ if (IsAResidualOfItsArgument(factor, factorScale, MathS.Settings.DowncastingEnabled))
return EDecimal.Zero;
var infinity = factor.IsNegative != negative ? EDecimal.NegativeInfinity : EDecimal.PositiveInfinity;
if (!y.IsFinite)
@@ -1126,6 +1132,50 @@ private static EDecimal TimesAHyperbolicPastTheExponents(EDecimal factor, EDecim
}
[ConstantField] private static readonly EDecimal lnTenAboveForTheFactor = EDecimal.FromString("2.3026");
+ ///
+ /// Whether , the sine or the cosine of an argument, is 0 for the exact argument: 0, or,
+ /// where , below the downcasting tolerance times , a
+ /// residual of the argument as on the real line, where cos(pi/2) is 0.
+ ///
+ private static bool IsAResidualOfItsArgument(EDecimal factor, EDecimal factorScale, bool readsResiduals)
+ {
+ if (factor.IsZero)
+ return true;
+ if (!readsResiduals)
+ return false;
+ // Decided from the decimal exponents first, which allocate nothing, wherever the factor is more than a
+ // digit either side of the bound: made exactly every time, the bound and the comparison were 3% more bytes
+ // on cos(1 + 2 i).
+ var tolerance = MathS.Settings.DowncastingToleranceAsDouble;
+ if (tolerance > 0 && Log10OfTheSize(factor) is { } size && Log10OfTheSize(factorScale) is { } scale)
+ {
+ var bound = scale + System.Math.Log10(tolerance);
+ if (size > bound + 1)
+ return false;
+ if (size < bound - 1)
+ return true;
+ }
+ return factor.Abs().LessThan(MathS.Settings.DowncastingTolerance.Multiply(factorScale.Abs()));
+
+ // log10 |value| to within a third of a digit, or null for 0, an infinity, NaN or an exponent past an int.
+ static double? Log10OfTheSize(EDecimal value)
+ => value.IsFinite && !value.IsZero && value.Exponent.CanFitInInt32()
+ ? value.Exponent.ToInt32Unchecked() + value.UnsignedMantissa.GetUnsignedBitLengthAsInt64() * log10OfTwo
+ : null;
+ }
+ private const double log10OfTwo = 0.30102999566398120;
+
+ ///
+ /// times , a part of sin(x + i y), cos(x + i y) or
+ /// e^(y + i x), which is 0 where the factor is a residual of x against . Read
+ /// against its own scale, the product was a residual only below the downcasting tolerance itself, where a real
+ /// part is read as 0, so from |y| near 116 a real part that is exactly 0 was cos(pi/2)'s residual
+ /// times cosh y: cos(pi/2 + 1000 i) had a real part of -8.8E+332. An imaginary part was read
+ /// as 0 at any size. https://github.com/asc-community/AngouriMath/issues/2700
+ ///
+ private static EDecimal TimesAHyperbolic(EDecimal factor, EDecimal factorScale, bool readsResiduals, EDecimal hyperbolic, EContext context)
+ => IsAResidualOfItsArgument(factor, factorScale, readsResiduals) ? EDecimal.Zero : factor.Multiply(hyperbolic, context);
+
/// Calculates the exact value of sine of num
public static Complex Sin(Complex num)
{
@@ -1145,9 +1195,10 @@ public static Complex Sin(Complex num)
if (cosh.IsInfinity())
return Complex.Create(TimesAHyperbolicPastTheExponents(sinOfRe, reScale, negative: false, im, context),
TimesAHyperbolicPastTheExponents(cosOfRe, reScale, negative: im.IsNegative, im, context));
+ var readsResiduals = MathS.Settings.DowncastingEnabled.Value;
return Complex.CreateAtScales(
- sinOfRe.Multiply(cosh, context),
- cosOfRe.Multiply(sinh, context),
+ TimesAHyperbolic(sinOfRe, reScale, readsResiduals, cosh, context),
+ TimesAHyperbolic(cosOfRe, reScale, readsResiduals, sinh, context),
reScale.Multiply(cosh, context), reScale.Multiply(sinh.Abs(), context));
}
@@ -1225,9 +1276,10 @@ public static Complex Cos(Complex num)
if (cosh.IsInfinity())
return Complex.Create(TimesAHyperbolicPastTheExponents(cosOfRe, reScale, negative: false, im, context),
TimesAHyperbolicPastTheExponents(sinOfRe, reScale, negative: !im.IsNegative, im, context));
+ var readsResiduals = MathS.Settings.DowncastingEnabled.Value;
return Complex.CreateAtScales(
- cosOfRe.Multiply(cosh, context),
- -sinOfRe.Multiply(sinh, context),
+ TimesAHyperbolic(cosOfRe, reScale, readsResiduals, cosh, context),
+ -TimesAHyperbolic(sinOfRe, reScale, readsResiduals, sinh, context),
reScale.Multiply(cosh, context), reScale.Multiply(sinh.Abs(), context));
}
@@ -1415,6 +1467,10 @@ internal static Complex ExpDifference(Complex v, Complex d)
return past.IsZero ? -Exp(v) : Exp(v + d);
var extra = 10 + DigitsBelowOne(x, y);
var work = Working(context, extra);
+ // A factor of either power that is a residual of its argument is 0, as Exp reads it: with the downcasting
+ // off, e^(2000 + pi i)'s sine was pi's residual times e^2000, and sinh(1000 + pi/2 i), which is i cosh(1000),
+ // had a real part of -8.8E+332. https://github.com/asc-community/AngouriMath/issues/2700
+ var readsResiduals = MathS.Settings.DowncastingEnabled.Value;
Complex power, lower, product;
// With the downcasting off: e^(-10^(-60)) is within it of 1 and was the integer 1 there.
using (MathS.Settings.DowncastingEnabled.Set(false))
@@ -1423,8 +1479,8 @@ internal static Complex ExpDifference(Complex v, Complex d)
// An exact d as it is: written out as decimals, 2 * 10^100 i was past the working digits, whose
// exponential has no value, and sinh(10^100 i) was NaN where it is i sin(10^100).
// https://github.com/asc-community/AngouriMath/issues/2685
- power = Exp(d.IsExact ? d : Complex.Create(x, y));
- lower = Exp(v);
+ power = Exp(d.IsExact ? d : Complex.Create(x, y), readsResiduals);
+ lower = Exp(v, readsResiduals);
product = lower * Complex.CreateAtScales(power.RealPart.EDecimal.Subtract(EDecimal.One, work), power.ImaginaryPart.EDecimal, EDecimal.Zero, EDecimal.Zero);
}
// Each sum rounded: taken exactly, 1 added to e^(10^7) was an integer of four million digits, and
diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
index ece874a19..86dbe0c27 100644
--- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
+++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
@@ -966,5 +966,44 @@ public void Issue2698_AnInexactRealPartPastItsDigitsStaysNaN(string expression)
{
Assert.Equal(MathS.NaN, expression.ToEntity().Evaled);
}
+
+ // A part that is 0 for the exact argument is 0 however large the other part: it was cos(pi/2)'s residual of pi's
+ // digits times cosh y, read as 0 only while the product was below the downcasting tolerance, so from |y| near 116
+ // cos(pi/2 + 1000 i) had a real part of -8.8E+332. Expected parts are mpmath's at 130 digits, "0" exactly 0.
+ // https://github.com/asc-community/AngouriMath/issues/2700
+ [Theory]
+ [InlineData("cos(pi/2 + 1000 * i)", "0", "-9.8503555700852349694443967612166156265846899266192289497640149569253192539122059673748903828151344496548190899e+433")]
+ [InlineData("cos(pi/2 + 120 * i)", "0", "-6.5209043919681613986693951404932440567230397078775663641572107633484275310902153794144056255698964113312389015e+51")]
+ [InlineData("cos(pi/2 + 10^5 * i)", "0", "-1.4033316802130615896591929092858713542681831352829432726937182671289326335243509263314615781457007441879829347e+43429")]
+ [InlineData("sin(pi + 1000 * i)", "0", "-9.8503555700852349694443967612166156265846899266192289497640149569253192539122059673748903828151344496548190899e+433")]
+ [InlineData("sin(-pi - 1000 * i)", "0", "9.8503555700852349694443967612166156265846899266192289497640149569253192539122059673748903828151344496548190899e+433")]
+ [InlineData("exp(1000 + pi/2 * i)", "0", "1.970071114017046993888879352243323125316937985323845789952802991385063850782441193474978076563026889930963818e+434")]
+ [InlineData("exp(300 - 3 * pi/2 * i)", "0", "1.9424263952412559365842088360176992193662086219516046941429177180671345272879182619666436840448422418235826784e+130")]
+ [InlineData("sinh(1000 + pi/2 * i)", "0", "9.8503555700852349694443967612166156265846899266192289497640149569253192539122059673748903828151344496548190899e+433")]
+ [InlineData("cosh(-1000 + pi/2 * i)", "0", "-9.8503555700852349694443967612166156265846899266192289497640149569253192539122059673748903828151344496548190899e+433")]
+ [InlineData("sec(pi/2 + 300 * i)", "0", "1.0296400444824027562309723842134261996269996448888708535082527816275329587150881380617907665242484454551632869e-130")]
+ public void Issue2700_APartThatIsZeroForTheExactArgumentIsZeroAtAnySize(string expression, string real, string imaginary)
+ => AssertTheParts(expression, real, imaginary);
+
+ // And a part that is small but not 0, its factor no residual of the argument, keeps its digits.
+ // https://github.com/asc-community/AngouriMath/issues/2700
+ [Theory]
+ [InlineData("cos(1/10^40 + 1000 * i)", "9.8503555700852349694443967612166156265846899266192289497640149569253192539122059181231125323889596024328352839e+433", "-9.8503555700852349694443967612166156265846899266192289497640149569253192539122059509576310993397428339141578212e+393")]
+ [InlineData("exp(1000 + 10^(-80) * i)", "1.970071114017046993888879352243323125316937985323845789952802991385063850782441193474978076563026889930963818e+434", "1.970071114017046993888879352243323125316937985323845789952802991385063850782441193474978076563026889930963818e+354")]
+ [InlineData("sinh(1000 + 10^(-40) * i)", "9.8503555700852349694443967612166156265846899266192289497640149569253192539122059181231125323889596024328352839e+433", "9.8503555700852349694443967612166156265846899266192289497640149569253192539122059509576310993397428339141578212e+393")]
+ public void Issue2700_ASmallPartThatIsNotZeroKeepsItsDigits(string expression, string real, string imaginary)
+ => AssertTheParts(expression, real, imaginary);
+
+ private static void AssertTheParts(string expression, string real, string imaginary)
+ {
+ var number = Assert.IsAssignableFrom(expression.ToEntity().Evaled);
+ foreach (var (part, truth) in new[] { (number.RealPart.EDecimal, real), (number.ImaginaryPart.EDecimal, imaginary) })
+ {
+ var expected = EDecimal.FromString(truth);
+ Assert.True(expected.IsZero ? part.IsZero
+ : part.IsFinite && part.Subtract(expected).Abs().CompareTo(expected.Abs().Multiply(EDecimal.FromString("1E-95"))) <= 0,
+ $"{expression} is {number}, where it is {real} + {imaginary}i");
+ }
+ }
}
}