Skip to content

Commit b035ed1

Browse files
arcsin and arccos of a real number past 1 have their value (#2245)
For about one real argument past 1 in ten, arcsin and arccos evaluated to NaN: arcsin(6 + pi), arccos(6 + pi), arcsin(1 + sqrt(3)). Of arcsin(k + c) for k from 1 to 40 and c in pi, e, sqrt(2), sqrt(3) and ln(3), 20 of the 200 were NaN, on 2.6.0 as on master. The value exists: sign(x) pi/2 - i arccosh|x|. Number.Arcsin and Number.Arccos share ArcSinCosInner. For w = x + iy it takes beta = (|w + 1| - |w - 1|) / 2, then arcsin(beta) or arccos(beta). By the triangle inequality, beta is in [-1, 1], and for a real x past 1 it is exactly 1. The two square roots are rounded, though, and for some x the difference comes out a little past 1, where the decimal arcsine is NaN. beta is now clamped to [-1, 1], which restores a bound the rounding broke and changes no value it did not. ArcsineOfARealPastOneTest takes arcsin and arccos at plus and minus (k + c) for those 200 arguments. It compares them with sign(x) pi/2 - i arccosh|x| and with 0 or pi plus i arccosh|x|, the library's convention, measured at 9.14 and -9.14. All five constants' cases fail on master. Found by work/compilesweep: the compiled arcsin(arcsin(x + pi)) at 6 had a value, and evaluation had none. Fixes #2244. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent bd750c4 commit b035ed1

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ and a number that passes every round is left open, since passing is not a proof
139139
| `"(10^30 + 1) in PP".ToEntity().Evaled` | `1000000000000000000000000000001 in PP` | `False` |
140140
| `"(10^30 + 57) in PP".ToEntity().Evaled` | `1000000000000000000000000000057 in PP` | `1000000000000000000000000000057 in PP` |
141141

142+
### `arcsin` and `arccos` of a real number past 1 have their value
143+
144+
**Silent: `arcsin(x)` and `arccos(x)` of a real `x` past 1 are `sign(x) pi/2 - i arccosh|x|` and `pi/2 - arcsin(x)`,
145+
where for about one argument in ten they were `NaN`.** The two share `(|w + 1| - |w - 1|) / 2`, which is in `[-1, 1]` by
146+
the triangle inequality and exactly 1 for such an `x`. Rounded, it came out a little past 1 for some `x`, and the decimal
147+
arcsine of that is `NaN`. Of `arcsin(k + c)` for `k` from 1 to 40 and `c` in `pi`, `e`, `sqrt(2)`, `sqrt(3)` and `ln(3)`,
148+
20 were `NaN` ([#2244](https://github.com/asc-community/AngouriMath/issues/2244)).
149+
150+
| Input | Was (2.6.0) | Now |
151+
|---|---|---|
152+
| `"arcsin(6 + pi)".ToEntity().Evaled` | `NaN` | `1.5707963267948966 - 2.902976737696685i`, to 15 digits |
153+
| `"arccos(6 + pi)".ToEntity().Evaled` | `NaN` | `2.902976737696685i`, to 15 digits |
154+
142155
### A system with a product equation is solved branch by branch
143156

144157
**Silent: solutions that were missing are answered.** `EquationSystem.Solve` eliminated `x * y = 0` for `y` as `0`,

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
929929
[Tests/UnitTests/Convenience/SignumOfNaNTest.cs]
930930
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
931931

932+
[Tests/UnitTests/Convenience/ArcsineOfARealPastOneTest.cs]
933+
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
934+
932935
[AngouriMath/Functions/Compilation/RealProducts.cs]
933936
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
934937

‎Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,15 @@ public static Complex Cotan(Complex num)
947947
var rho = xp1.MultiplyAndAdd(xp1, y.Multiply(y, context), context).SqrtByIntegerRoot(context);
948948
var sigma = xm1.MultiplyAndAdd(xm1, y.Multiply(y, context), context).SqrtByIntegerRoot(context);
949949
var alpha = rho.Add(sigma, context).Divide(2, context);
950-
return (rho.Subtract(sigma, context).Divide(2, context),
950+
// beta is (|w + 1| - |w - 1|) / 2, in [-1, 1] by the triangle inequality and exactly 1 for a real w past
951+
// 1; the two roots are rounded, and a beta of 1 + ε made arcsin(beta) NaN, as arcsin(6 + pi) was.
952+
// https://github.com/asc-community/AngouriMath/issues/2244
953+
var beta = rho.Subtract(sigma, context).Divide(2, context);
954+
if (beta.GreaterThan(EDecimal.One))
955+
beta = EDecimal.One;
956+
else if (beta.LessThan(-EDecimal.One))
957+
beta = -EDecimal.One;
958+
return (beta,
951959
alpha.MultiplyAndSubtract(alpha, EDecimal.One, context).SqrtByIntegerRoot(context).Add(alpha, context).NaturalLogarithm(context).Multiply((y.IsNegative || y.IsZero) ? -1 : 1, context));
952960
}
953961

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.Numerics;
9+
using AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Convenience
14+
{
15+
/// <summary>
16+
/// <c>arcsin</c> and <c>arccos</c> of a real number past 1 have their value, <c>sign(x) pi/2 - i arccosh|x|</c> and
17+
/// <c>pi/2 - arcsin(x)</c>, where for one argument in ten they were NaN: the rounded <c>(|w + 1| - |w - 1|) / 2</c>
18+
/// came out a little past 1, whose decimal arcsine is NaN. <c>arcsin(6 + pi)</c> was one.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2244">#2244</a>
20+
/// </summary>
21+
[Trait("Area", "Convenience")]
22+
public sealed class ArcsineOfARealPastOneTest
23+
{
24+
private static double Arccosh(double x) => System.Math.Log(x + System.Math.Sqrt(x * x - 1));
25+
26+
private static bool Agree(Complex expected, Complex actual) => Complex.Abs(expected - actual) <= 1e-12 * System.Math.Max(1, Complex.Abs(expected));
27+
28+
[Theory]
29+
[InlineData("pi")]
30+
[InlineData("e")]
31+
[InlineData("sqrt(2)")]
32+
[InlineData("sqrt(3)")]
33+
[InlineData("ln(3)")]
34+
public void BothHaveTheirValue(string constant)
35+
{
36+
for (var k = 1; k <= 40; k++)
37+
foreach (var sign in new[] { "", "-" })
38+
{
39+
var argument = $"{sign}({k} + {constant})";
40+
var x = ((Entity.Number.Complex)argument.ToEntity().Evaled).ToNumerics().Real;
41+
if (System.Math.Abs(x) <= 1)
42+
continue;
43+
var arcsin = ((Entity.Number.Complex)$"arcsin({argument})".ToEntity().Evaled).ToNumerics();
44+
var arccos = ((Entity.Number.Complex)$"arccos({argument})".ToEntity().Evaled).ToNumerics();
45+
var expectedArcsin = new Complex(System.Math.Sign(x) * System.Math.PI / 2, -Arccosh(System.Math.Abs(x)));
46+
var expectedArccos = new Complex(x > 0 ? 0 : System.Math.PI, Arccosh(System.Math.Abs(x)));
47+
Assert.True(Agree(expectedArcsin, arcsin), $"arcsin({argument}): {arcsin}, where {expectedArcsin}");
48+
Assert.True(Agree(expectedArccos, arccos), $"arccos({argument}): {arccos}, where {expectedArccos}");
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)