Skip to content

Commit aef5535

Browse files
tanh and coth of a large number are their limits (#2305)
tanh and coth are spelled (e^(2x) - 1) / (e^(2x) + 1) and its reciprocal. Past the range of the decimals both sides are infinite, so tanh(5568) and coth(332640) evaluated to NaN, where they are 1; past a double's, tanh(400) compiled to NaN in all three compilers. The negative side was right only because e^(-11136) is 0. A quotient of one power plus two constants, (b^v + c1) / (b^v + c2), is the same value as 1 + (c1 - c2) / (b^v + c2) wherever it has one, and the second does not divide an overflow by an overflow. ExponentialQuotients writes it so. Evaluation asks for it where both sides of a quotient come out infinite, and the compilers' pre-pass writes every such quotient so, since a double cannot tell an overflow from an infinity. At +oo it is the limit, 1, as tanh(-oo) was already -1 and arctan(+oo) is pi/2. Respelling tanh itself as 1 - 2/(e^(2x) + 1) was measured and not taken: 21 tests change, nearly all integrals of hyperbolic functions the integrator reads in the quotient form. HyperbolicTangentPastTheRangeTest takes both sides evaluated and compiled, the infinities, and the compiled values inside the range, near 0 as well, against Math.Tanh. Seven of its cases fail on master. compilesweep's 112 findings are the same list on master and here. The suite, the trio, F#, AOT and the eight harnesses pass. Fixes #2254. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c920408 commit aef5535

7 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,23 @@ made `0^(-1)` an infinity and `0^0` 1 ([#2286](https://github.com/asc-community/
442442
| `"x^(-1)".Compile<double, double>("x")(0)` | `+Infinity` | `NaN` |
443443
| `"x^x".Compile<double, double>("x")(0)` | `1` | `NaN` |
444444

445+
### `tanh` and `coth` of a large number are their limits
446+
447+
**Silent: `tanh` and `coth` of a large positive number evaluate and compile to `1`, where they were `NaN`, and at
448+
`+oo` they are `1`, as at `-oo` they were already `-1`.** Both are spelled `(e^(2x) - 1) / (e^(2x) + 1)` and its
449+
reciprocal, and past the range of the decimals, or of a double, both sides were infinite. A quotient of one power plus
450+
two constants, `(b^v + c1) / (b^v + c2)`, is now evaluated as `1 + (c1 - c2) / (b^v + c2)` where both sides are
451+
infinite, and always compiled so, since a double cannot tell an overflow from an infinity
452+
([#2254](https://github.com/asc-community/AngouriMath/issues/2254)).
453+
454+
| Input | Was (2.6.0) | Now |
455+
|---|---|---|
456+
| `"tanh(5568)".ToEntity().Evaled` | `NaN` | `1` |
457+
| `"coth(332640)".ToEntity().EvalNumerical()` | `NaN` | `1` |
458+
| `"tanh(+oo)".ToEntity().Evaled` | `NaN` | `1` |
459+
| `"tanh(x)".Compile<double, double>("x")(400)` | `NaN` | `1` |
460+
| `"coth(x)".ToEntity().Compile("x").Call(400)` | `(NaN, NaN)` | `(1, 0)` |
461+
445462
### A lambda applied to a vector multiplies by it as a vector
446463

447464
**Silent: a matrix of vectors becomes the product.** Applying a lambda simplified its body first, and a simplified

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
435435
[AngouriMath/Numerics/NumberStatements.cs]
436436
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
437437

438+
[AngouriMath/Functions/ExponentialQuotients.cs]
439+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
440+
438441
[AngouriMath/Functions/Compilation/CompiledMembership.cs]
439442
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
440443

@@ -1016,6 +1019,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
10161019
[Tests/UnitTests/Common/DivisionByZeroOverDoubleTest.cs]
10171020
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
10181021

1022+
[Tests/UnitTests/Common/HyperbolicTangentPastTheRangeTest.cs]
1023+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
1024+
10191025
[Tests/UnitTests/Common/RefuseNonRealCompilationTest.cs]
10201026
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
10211027

‎Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ internal static FastExpression Compile(Entity func, IEnumerable<Variable> variab
10161016
foreach (var constant in Variable.NamedConstants.Values)
10171017
func = func.Substitute(constant, constant.Value);
10181018
func = Compilation.CompiledMembership.Rewritten(func);
1019+
func = Functions.ExponentialQuotients.Rewritten(func);
10191020
Compilation.StatementPositions.Check(func);
10201021
var visited = new HashSet<Entity>();
10211022
var cache = new Dictionary<Entity, int>();

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/IntoLinqCompiler.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal static TDelegate Compile<TDelegate>(
3131
if (expr.Nodes.Any(node => node is Entity.Matrix))
3232
expr = expr.InnerSimplified;
3333
expr = CompiledMembership.Rewritten(expr);
34+
expr = Functions.ExponentialQuotients.Rewritten(expr);
3435
StatementPositions.Check(expr);
3536
if (MathS.Settings.RefuseNonRealCompilation && returnType is { } real && IsRealType(real)
3637
&& typesAndNames.All(c => IsRealType(c.type)) && !IsRealWhereverDefined(expr, typesAndNames))

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ n0.Evaled is Complex c
239239
? c.IsZero ? MathS.NaN : 0
240240
: new Providedf(0, new Notf(new Equalsf(n0, 0))),
241241
(_, Integer(0)) => Real.NaN,
242+
// tanh's spelling past the decimals' range, both sides infinite: the same
243+
// quotient without them. https://github.com/asc-community/AngouriMath/issues/2254
244+
(Complex { IsFinite: false }, Complex { IsFinite: false }) when !isExact
245+
&& Functions.ExponentialQuotients.Rescaled(Dividend, Divisor) is { } rescaled
246+
=> rescaled.InnerSimplified(isExact),
242247
(Complex n1, Complex n2) when !isExact => n1 / n2,
243248
(var n1, Integer(1)) => n1,
244249
// Sizes are not divided: aleph(0) / 2 has no value.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 static AngouriMath.Entity;
9+
10+
namespace AngouriMath.Functions
11+
{
12+
/// <summary>
13+
/// A quotient of one power plus two constants, <c>(b^v + c1) / (b^v + c2)</c>, as
14+
/// <c>1 + (c1 - c2) / (b^v + c2)</c>. The two are the same value wherever the quotient has one,
15+
/// and the second does not divide an overflow by an overflow.
16+
/// </summary>
17+
/// <remarks>
18+
/// It is how <c>tanh</c> and <c>coth</c> are spelled: <see cref="MathS.Hyperbolic.Tanh"/> is
19+
/// <c>(e^(2x) - 1) / (e^(2x) + 1)</c>. Past the range of the decimals, or of a double, both sides
20+
/// of it are infinite, so <c>tanh(5568)</c> evaluated to NaN and <c>tanh(400)</c> compiled to NaN,
21+
/// where both are 1. At a real infinity the second form is the limit, as <c>tanh(-oo)</c> already
22+
/// was <c>-1</c> and <c>arctan(+oo)</c> is <c>pi / 2</c>: <c>tanh(+oo)</c> is 1.
23+
/// https://github.com/asc-community/AngouriMath/issues/2254
24+
/// </remarks>
25+
internal static class ExponentialQuotients
26+
{
27+
/// <summary>
28+
/// <c>1 + (c1 - c2) / divisor</c> where <paramref name="dividend"/> and
29+
/// <paramref name="divisor"/> are one power plus a constant each, and <see langword="null"/>
30+
/// otherwise.
31+
/// </summary>
32+
internal static Entity? Rescaled(Entity dividend, Entity divisor)
33+
=> Split(dividend) is var (power, c1) && Split(divisor) is var (same, c2) && power == same
34+
? 1 + (c1 - c2) / divisor
35+
: null;
36+
37+
/// <summary>
38+
/// Every such quotient in <paramref name="expression"/> rewritten, for the compilers: a double
39+
/// cannot tell an overflow from an infinity, so the rewrite is made wherever the shape is.
40+
/// </summary>
41+
internal static Entity Rewritten(Entity expression)
42+
=> expression.Nodes.Any(node => node is Divf(Sumf or Minusf, Sumf or Minusf))
43+
? expression.Replace(node => node is Divf(var dividend, var divisor) && Rescaled(dividend, divisor) is { } rescaled ? rescaled : node)
44+
: expression;
45+
46+
/// <summary>The power and the constant of <c>b^v + c</c>, <c>c + b^v</c> or <c>b^v - c</c>, with <c>b</c> and <c>c</c> constants.</summary>
47+
private static (Powf Power, Entity Constant)? Split(Entity sum) => sum switch
48+
{
49+
Sumf(Powf power, var constant) when IsConstant(power.Base) && IsConstant(constant) => (power, constant),
50+
Sumf(var constant, Powf power) when IsConstant(power.Base) && IsConstant(constant) => (power, constant),
51+
Minusf(Powf power, var constant) when IsConstant(power.Base) && IsConstant(constant) => (power, -constant),
52+
_ => null,
53+
};
54+
55+
private static bool IsConstant(Entity expression) => !expression.Vars.Any();
56+
}
57+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)