|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Numerics; |
| 10 | +using AngouriMath; |
| 11 | +using AngouriMath.Extensions; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace AngouriMath.Tests.Common |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// <c>tanh</c> and <c>coth</c> are spelled <c>(e^(2x) - 1) / (e^(2x) + 1)</c> and its reciprocal, and past the range |
| 18 | + /// of the decimals, or of a double, both sides are infinite: <c>tanh(5568)</c> evaluated to NaN and <c>tanh(400)</c> |
| 19 | + /// compiled to NaN, where both are 1. The negative side was right, since <c>e^(-11136)</c> is 0. |
| 20 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2254">#2254</a> |
| 21 | + /// </summary> |
| 22 | + [Trait("Area", "Common")] |
| 23 | + public sealed class HyperbolicTangentPastTheRangeTest |
| 24 | + { |
| 25 | + [Theory] |
| 26 | + [InlineData("tanh(5568)", 1)] |
| 27 | + [InlineData("tanh(-5568)", -1)] |
| 28 | + [InlineData("coth(332640)", 1)] |
| 29 | + [InlineData("coth(-332640)", -1)] |
| 30 | + [InlineData("tanh(+oo)", 1)] |
| 31 | + [InlineData("tanh(-oo)", -1)] |
| 32 | + [InlineData("coth(+oo)", 1)] |
| 33 | + public void EvaluatedPastTheRangeItIsOne(string expression, int expected) |
| 34 | + { |
| 35 | + Assert.Equal(Entity.Number.Integer.Create(expected), expression.ToEntity().Evaled); |
| 36 | + Assert.Equal(Entity.Number.Integer.Create(expected), expression.ToEntity().EvalNumerical()); |
| 37 | + } |
| 38 | + |
| 39 | + [Theory] |
| 40 | + [InlineData("tanh(x)", 400, 1)] |
| 41 | + [InlineData("tanh(x)", -400, -1)] |
| 42 | + [InlineData("coth(x)", 400, 1)] |
| 43 | + [InlineData("coth(x)", -400, -1)] |
| 44 | + [InlineData("tanh(x)", double.PositiveInfinity, 1)] |
| 45 | + [InlineData("tanh(x)", double.NegativeInfinity, -1)] |
| 46 | + public void CompiledPastTheRangeItIsOne(string expression, double at, double expected) |
| 47 | + { |
| 48 | + Assert.Equal(expected, expression.Compile<double, double>("x")(at)); |
| 49 | + Assert.Equal(new Complex(expected, 0), expression.Compile<Complex, Complex>("x")(at)); |
| 50 | + Assert.Equal(new Complex(expected, 0), expression.Compile("x").Call(at)); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary>Inside the range the compiled values are still the hyperbolic tangent's, near 0 as well.</summary> |
| 54 | + [Theory] |
| 55 | + [InlineData(1e-6)] |
| 56 | + [InlineData(-0.3)] |
| 57 | + [InlineData(2)] |
| 58 | + [InlineData(19)] |
| 59 | + public void CompiledInsideTheRangeItIsTheHyperbolicTangent(double at) |
| 60 | + { |
| 61 | + var compiled = "tanh(x)".Compile<double, double>("x")(at); |
| 62 | + Assert.True(Math.Abs(compiled - Math.Tanh(at)) <= 1e-9 * Math.Abs(Math.Tanh(at)), $"{compiled} against {Math.Tanh(at)}"); |
| 63 | + var coth = "coth(x)".Compile<double, double>("x")(at); |
| 64 | + Assert.True(Math.Abs(coth - 1 / Math.Tanh(at)) <= 1e-9 * Math.Abs(1 / Math.Tanh(at)), $"{coth} against {1 / Math.Tanh(at)}"); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments