diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index b942ff1ca..9a55f9536 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -293,6 +293,37 @@ the logarithm of a negative number gives, where mpmath takes the other side of t NaN and `artanh(-1)` and `arcoth(-1)` are `-oo`, on both. The last digit of some printed values moves, and a real part that is 0 on the unit circle, `arcosh(1 - 10^(-30))` among them, stays 0. +### cos, sin and tan of a large imaginary argument are their infinities, or i + +**Silent: `NaN` becomes a value.** From an imaginary part near `230259`, `cosh` and `sinh` of it are past the +exponents and infinite, and a part of `cos(x + i y)` or `sin(x + i y)` that is `0` times that infinity was `NaN`, and +the number with it: `cos(10^6 i)` was `NaN` where `cosh(10^6)` is `+oo`. A part is now `0` where the cosine or the +sine of `x` is `0`, or a residual of `x` as on the real line, and otherwise the infinity of its sign, or the number it +is where that factor is small enough to bring it back within the exponents. A direction off the axes is written as +`exp(10^6 + i)` already writes it, each part the infinity of its sign. `tan(x + i y)` took `tanh(2 y)` through +`e^(2 y)`, `+oo / +oo`, and was `NaN` where it is `±i` to every working digit, and `sec` and `csc` of an infinity off +the real line were `NaN` where they are `0`. A decimal real part past the digits its reduction needs, +`10^200 sqrt(2) + 10^6 i`, leaves the direction unknown and is still `NaN`. Measured on master (82eb869e1) and on +this branch; the true values are mpmath's at 450 digits +([#2698](https://github.com/asc-community/AngouriMath/issues/2698)). + +| `.Evaled` | Was (master) | Now | +|---|---|---| +| `cos(10^6 * i)` | `NaN` | `+oo` | +| `sin(-10^6 * i)` | `NaN` | `-ooi` | +| `tan(10^6 * i)` | `NaN` | `i` | +| `tan(1/2 - 10^300 * i)` | `NaN` | `-i` | +| `sec(1 + 10^6 * i)` | `NaN` | `0` | +| `csc(10^6 * i)` | `NaN` | `0` | +| `cos(pi/2 + 10^6 * i)` | `-oo - +ooi` | `-ooi` | +| `cos(1/10^40 + 230300 * i)` | `+oo - +ooi` | `+oo - 5.2257944491…E+99977i` | + +Of 2184 values of `cos`, `sin`, `tan`, `cot`, `sec`, `csc`, `cosh`, `sinh`, `tanh`, `coth`, `sech`, `csch` and `exp` +of `±10^k i` and `a ± 10^k i` for `a` in `1`, `-1`, `1/2` and `pi/4` and `k` from 1 to 12, 50, 100 and 300, and of +eighteen ordinary arguments, master had 368 `NaN`; this branch has 300 of them right to a relative `1e-90` and 40 the +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 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 b7de321bb..4ede9985f 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs @@ -1092,6 +1092,40 @@ internal static Complex WithinTheSpecialFunctionsReach(Complex z) } [ConstantField] private static readonly EDecimal specialFunctionsReach = EDecimal.FromString("1E+1000"); + /// + /// A part of sin(x + i y) or cos(x + i y), -- the sine or the cosine of + /// x -- times cosh y or sinh y, where those are past the context's exponents and infinite. + /// The product of a zero factor and the infinity was NaN, and with it the whole number: cos(10^6 i) was + /// NaN where it is cosh(10^6), +oo. A factor that is 0, or a residual of x against + /// as on the real line, makes the part 0, so that cos(pi/2 + 10^6 i) is + /// -oo i; any other makes it the infinity of the factor's sign, turned round where + /// says the rest of the part is negative, unless |factor| e^|y| / 2 is still + /// within the exponents, where it is that number. https://github.com/asc-community/AngouriMath/issues/2698 + /// + private static EDecimal TimesAHyperbolicPastTheExponents(EDecimal factor, EDecimal factorScale, bool negative, EDecimal y, EContext context) + { + if (factor.IsNaN() || y.IsNaN()) + return EDecimal.NaN; + if (factor.IsZero || MathS.Settings.DowncastingEnabled && factor.Abs().LessThan(MathS.Settings.DowncastingTolerance.Multiply(factorScale.Abs()))) + return EDecimal.Zero; + var infinity = factor.IsNegative != negative ? EDecimal.NegativeInfinity : EDecimal.PositiveInfinity; + if (!y.IsFinite) + return infinity; + // |factor| cosh y is e^(|y| + ln |factor| - ln 2) to a relative e^(-2 |y|). Its exponent is first bounded + // below from the factor's decimal exponent, which decides every factor that is not near a residual. + var size = y.Abs(); + var adjusted = factor.Exponent.Add(factor.UnsignedMantissa.GetDigitCountAsEInteger()).Subtract(EInteger.One); + var below = size.Add(EDecimal.FromEInteger(adjusted).Multiply(lnTenAboveForTheFactor, context), context).Subtract(EDecimal.One, context); + if (InternalAMExtensions.PastTheExponents(below, context) is { IsZero: false }) + return infinity; + var digits = size.Exponent.Add(size.UnsignedMantissa.GetDigitCountAsEInteger()); + var work = EContext.ForPrecision(context.Precision.Add(digits.Sign > 0 ? digits : EInteger.Zero).Add(10).ToInt32Checked()).WithUnlimitedExponents(); + var exponent = size.Add(factor.Abs().NaturalLogarithm(work), work).Subtract(EDecimal.FromInt32(2).NaturalLogarithm(work), work); + var magnitude = InternalAMExtensions.PastTheExponents(exponent, context) ?? exponent.Exponential(context); + return infinity.IsNegative ? magnitude.Negate() : magnitude; + } + [ConstantField] private static readonly EDecimal lnTenAboveForTheFactor = EDecimal.FromString("2.3026"); + /// Calculates the exact value of sine of num public static Complex Sin(Complex num) { @@ -1107,6 +1141,10 @@ public static Complex Sin(Complex num) // is one of them times sinh(im) or cosh(im): sin(pi + 1e-60 i) is -1e-60 i, and was 0. var (sinOfRe, cosOfRe) = SinAndCosOfAReal(num.RealPart, re, context); var reScale = PeriodicResidualScale(num.RealPart, re, context); + // Past the exponents cosh(im) is +oo, and sinh(im) the infinity of its sign. + if (cosh.IsInfinity()) + return Complex.Create(TimesAHyperbolicPastTheExponents(sinOfRe, reScale, negative: false, im, context), + TimesAHyperbolicPastTheExponents(cosOfRe, reScale, negative: im.IsNegative, im, context)); return Complex.CreateAtScales( sinOfRe.Multiply(cosh, context), cosOfRe.Multiply(sinh, context), @@ -1114,12 +1152,25 @@ public static Complex Sin(Complex num) } /// Calculates the exact value of secant of num + /// + /// Where cos is past the exponents, its reciprocal is below them and 0, as e^(-10^6) is: a + /// quotient by an infinity off the real line is NaN, and sec(1 + 10^6 i) was. + /// https://github.com/asc-community/AngouriMath/issues/2698 + /// public static Complex Secant(Complex num) - => 1 / Cos(num); + => Reciprocal(Cos(num)); /// Calculates the exact value of cosecant of num + /// As for : csc(10^6 i) is 0, and was NaN. public static Complex Cosecant(Complex num) - => 1 / Sin(num); + => Reciprocal(Sin(num)); + + /// 1 / value, which is 0 where a part of is infinite and none is NaN. + private static Complex Reciprocal(Complex value) + { + var (re, im) = (value.RealPart.EDecimal, value.ImaginaryPart.EDecimal); + return (re.IsInfinity() || im.IsInfinity()) && !re.IsNaN() && !im.IsNaN() ? Integer.Zero : 1 / value; + } /// /// sec(x) = value @@ -1171,6 +1222,9 @@ public static Complex Cos(Complex num) // As for Sin: cos(pi/2 + 1e-60 i) is -1e-60 i, and was 0. var (sinOfRe, cosOfRe) = SinAndCosOfAReal(num.RealPart, re, context); var reScale = PeriodicResidualScale(num.RealPart, re, context); + if (cosh.IsInfinity()) + return Complex.Create(TimesAHyperbolicPastTheExponents(cosOfRe, reScale, negative: false, im, context), + TimesAHyperbolicPastTheExponents(sinOfRe, reScale, negative: !im.IsNegative, im, context)); return Complex.CreateAtScales( cosOfRe.Multiply(cosh, context), -sinOfRe.Multiply(sinh, context), diff --git a/Sources/AngouriMath/Functions/InternalAMExtensions.cs b/Sources/AngouriMath/Functions/InternalAMExtensions.cs index 281e544e6..e976a4a70 100644 --- a/Sources/AngouriMath/Functions/InternalAMExtensions.cs +++ b/Sources/AngouriMath/Functions/InternalAMExtensions.cs @@ -1237,6 +1237,11 @@ public static EDecimal Tanh(this EDecimal x, EContext context) return EDecimal.NaN; if (x.IsInfinity()) return x.Sign; + // Past (digits + 3) ln 10, 1 - |tanh x| = 2 / (e^(2|x|) + 1) is below the last digit, and tanh x is its sign: + // taken through e^x, past the exponents it was +oo / +oo, NaN, and tan(10^6 i) with it. + // https://github.com/asc-community/AngouriMath/issues/2698 + if (!context.Precision.IsZero && x.Abs().CompareTo(EDecimal.FromEInteger(context.Precision.Add(3)).Multiply(lnTenAbove)) > 0) + return x.Sign; var y = x.Exponential(context); var yy = EDecimal.One.Divide(y, context); return y.Subtract(yy, context).Divide(y.Add(yy, context), context); diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs index cfd3b0469..ece874a19 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -892,5 +892,79 @@ public void Issue2689_TheAreaFunctionsNearTheirZeroKeepTheirDigits(string expres $"{expression} is {value}, where it is {trueReal} + {trueImaginary}i"); } } + + // Past the exponents, near 10^100000, cosh y is +oo, and a part of cos(x + i y) or sin(x + i y) that is 0 times it + // was NaN, and the number with it: cos(10^6 i) was NaN where cosh(10^6) is +oo. A part whose factor is 0, or a + // residual of x as on the real line, is 0; any other is the infinity of its sign. tan(x + i y) took tanh(2 y) + // through e^(2 y), +oo over +oo, and was NaN where it is i to every working digit; sec and csc of an infinity are + // 0. Each expected part is "+oo", "-oo" or the number it is. https://github.com/asc-community/AngouriMath/issues/2698 + [Theory] + [InlineData("cos(10^6 * i)", "+oo", "0")] + [InlineData("cos(-10^6 * i)", "+oo", "0")] + [InlineData("cos(10^10 * i)", "+oo", "0")] + [InlineData("cos(10^1200 * i)", "+oo", "0")] + [InlineData("sin(10^6 * i)", "0", "+oo")] + [InlineData("sin(-10^300 * i)", "0", "-oo")] + [InlineData("cos(1 + 10^6 * i)", "+oo", "-oo")] + [InlineData("cos(-1 - 10^6 * i)", "+oo", "-oo")] + [InlineData("sin(-1 - 10^6 * i)", "-oo", "-oo")] + [InlineData("cos(pi/2 + 10^6 * i)", "0", "-oo")] + [InlineData("sin(pi + 10^6 * i)", "0", "-oo")] + [InlineData("cos(10^200 + 10^6 * i)", "-oo", "-oo")] + [InlineData("tan(10^6 * i)", "0", "1")] + [InlineData("tan(-10^6 * i)", "0", "-1")] + [InlineData("tan(1 + 10^6 * i)", "0", "1")] + [InlineData("tan(1/2 - 10^300 * i)", "0", "-1")] + [InlineData("tan(pi/4 + 10^10 * i)", "0", "1")] + [InlineData("sec(10^6 * i)", "0", "0")] + [InlineData("sec(1 + 10^6 * i)", "0", "0")] + [InlineData("csc(-10^6 * i)", "0", "0")] + [InlineData("csc(-1 + 10^50 * i)", "0", "0")] + public void Issue2698_TheTrigonometricFunctionsPastTheExponentsAreTheirInfinities(string expression, string real, string imaginary) + { + var value = expression.ToEntity().Evaled; + var number = Assert.IsAssignableFrom(value); + foreach (var (part, truth) in new[] { (number.RealPart.EDecimal, real), (number.ImaginaryPart.EDecimal, imaginary) }) + Assert.True(truth switch + { + "+oo" => part.IsPositiveInfinity(), + "-oo" => part.IsNegativeInfinity(), + _ => part.IsFinite && part.CompareTo(EDecimal.FromString(truth)) == 0 + }, $"{expression} is {value}, where it is {real} + {imaginary}i"); + } + + // A part whose factor brings it back within the exponents is that number: here sin(10^-40) e^230300 / 2, held to + // a relative 1e-95 of mpmath's at 130 digits, beside a part past them. + // https://github.com/asc-community/AngouriMath/issues/2698 + [Theory] + [InlineData("cos(1/10^40 + 230300 * i)", "+oo", "-5.2257944491284101600284911996641083214717482575178363910478813369466065521343777714526494005126125209332974077e+99977")] + [InlineData("sin(1/10^40 - 230300 * i)", "5.2257944491284101600284911996641083214717482575178363910478813369466065521343777714526494005126125209332974077e+99977", "-oo")] + public void Issue2698_APartItsFactorBringsWithinTheExponentsIsANumber(string expression, string real, string imaginary) + { + var value = expression.ToEntity().Evaled; + var number = Assert.IsAssignableFrom(value); + foreach (var (part, truth) in new[] { (number.RealPart.EDecimal, real), (number.ImaginaryPart.EDecimal, imaginary) }) + { + if (truth is "+oo" or "-oo") + { + Assert.True(truth == "+oo" ? part.IsPositiveInfinity() : part.IsNegativeInfinity(), $"{expression} is {value}"); + continue; + } + var expected = EDecimal.FromString(truth); + Assert.True(part.IsFinite && part.Subtract(expected).Abs().CompareTo(expected.Abs().Multiply(EDecimal.FromString("1E-95"))) <= 0, + $"{expression} is {value}, where it is {real} + {imaginary}i"); + } + } + + // An inexact real part past the digits its reduction by 2 pi needs leaves the factor unknown, and the direction + // with it, however large the imaginary part. https://github.com/asc-community/AngouriMath/issues/2698 + [Theory] + [InlineData("cos(10^200 * sqrt(2) + 10^6 * i)")] + [InlineData("sin(10^200 * sqrt(2) - 10^6 * i)")] + [InlineData("tan(10^200 * sqrt(2) + 10^6 * i)")] + public void Issue2698_AnInexactRealPartPastItsDigitsStaysNaN(string expression) + { + Assert.Equal(MathS.NaN, expression.ToEntity().Evaled); + } } }