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
31 changes: 31 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
58 changes: 56 additions & 2 deletions Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,40 @@ internal static Complex WithinTheSpecialFunctionsReach(Complex z)
}
[ConstantField] private static readonly EDecimal specialFunctionsReach = EDecimal.FromString("1E+1000");

/// <summary>
/// A part of <c>sin(x + i y)</c> or <c>cos(x + i y)</c>, <paramref name="factor"/> -- the sine or the cosine of
/// <c>x</c> -- times <c>cosh y</c> or <c>sinh y</c>, 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: <c>cos(10^6 i)</c> was
/// NaN where it is <c>cosh(10^6)</c>, <c>+oo</c>. A factor that is 0, or a residual of <c>x</c> against
/// <paramref name="factorScale"/> as on the real line, makes the part 0, so that <c>cos(pi/2 + 10^6 i)</c> is
/// <c>-oo i</c>; any other makes it the infinity of the factor's sign, turned round where
/// <paramref name="negative"/> says the rest of the part is negative, unless <c>|factor| e^|y| / 2</c> is still
/// within the exponents, where it is that number. https://github.com/asc-community/AngouriMath/issues/2698
/// </summary>
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");

/// <summary>Calculates the exact value of sine of num</summary>
public static Complex Sin(Complex num)
{
Expand All @@ -1107,19 +1141,36 @@ 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),
reScale.Multiply(cosh, context), reScale.Multiply(sinh.Abs(), context));
}

/// <summary>Calculates the exact value of secant of num</summary>
/// <remarks>
/// Where <c>cos</c> is past the exponents, its reciprocal is below them and 0, as <c>e^(-10^6)</c> is: a
/// quotient by an infinity off the real line is NaN, and <c>sec(1 + 10^6 i)</c> was.
/// https://github.com/asc-community/AngouriMath/issues/2698
/// </remarks>
public static Complex Secant(Complex num)
=> 1 / Cos(num);
=> Reciprocal(Cos(num));

/// <summary>Calculates the exact value of cosecant of num</summary>
/// <remarks>As for <see cref="Secant(Complex)"/>: <c>csc(10^6 i)</c> is 0, and was NaN.</remarks>
public static Complex Cosecant(Complex num)
=> 1 / Sin(num);
=> Reciprocal(Sin(num));

/// <summary><c>1 / value</c>, which is 0 where a part of <paramref name="value"/> is infinite and none is NaN.</summary>
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;
}

/// <summary>
/// sec(x) = value
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions Sources/AngouriMath/Functions/InternalAMExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
74 changes: 74 additions & 0 deletions Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
{ 20692733745923116440.0, 985012471441596398530723840.0, 53035823659846109665657533505732608.0, 3088144109088191970827418502918634458841088.0 },
{ 985012471441596398530723840.0, 53035823659846109665657533505732608.0, 3088144109088191970827418502918634458841088.0, 190454802288659943182846234171424053458511757049856.0 }
});
var product = (Entity.Matrix)(matrix * matrix.Inverse).InnerSimplified;

Check warning on line 279 in Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Possible null reference argument for parameter 'm2' in 'Matrix Matrix.operator *(Matrix m1, Matrix m2)'.
Assert.Equal(MathS.IdentityMatrix(4), product);
}

Expand Down Expand Up @@ -892,5 +892,79 @@
$"{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<Entity.Number.Complex>(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<Entity.Number.Complex>(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);
}
}
}
Loading