diff --git a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs index aa1ff5b..b672b9a 100644 --- a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs +++ b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs @@ -18,6 +18,7 @@ ce.RegisterFunction("ACOS", 1, Acos); ce.RegisterFunction("ACOSH", 1, Acosh); ce.RegisterFunction("ACOT", 1, Acot); + ce.RegisterFunction("ACOTH", 1, Acoth); ce.RegisterFunction("ASIN", 1, Asin); ce.RegisterFunction("ASINH", 1, Asinh); ce.RegisterFunction("ATAN", 1, Atan); @@ -496,6 +497,15 @@ return x; } + private static object Acoth(List p) + { + double number = p[0]; + if (Math.Abs(number) < 1) + throw new NumberException(); + + return 0.5 * Math.Log((number + 1) / (number - 1)); + } + private static object Asinh(List p) { return XLMath.ASinh(p[0]); diff --git a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs index ec28464..4232028 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs @@ -39,6 +39,36 @@ Assert.AreEqual(expectedResult, actual, tolerance * 10); } + [TestCase(-10, -0.100335348)] + [TestCase(-9, -0.111571776)] + [TestCase(-8, -0.125657214)] + [TestCase(-7, -0.143841036)] + [TestCase(-6, -0.168236118)] + [TestCase(-5, -0.202732554)] + [TestCase(-4, -0.255412812)] + [TestCase(-3, -0.34657359)] + [TestCase(-2, -0.549306144)] + [TestCase(2, 0.549306144)] + [TestCase(3, 0.34657359)] + [TestCase(4, 0.255412812)] + [TestCase(5, 0.202732554)] + [TestCase(6, 0.168236118)] + [TestCase(7, 0.143841036)] + [TestCase(8, 0.125657214)] + [TestCase(9, 0.111571776)] + [TestCase(10, 0.100335348)] + public void Acoth_ReturnsCorrectValue(double input, double expectedResult) + { + var actual = (double)XLWorkbook.EvaluateExpr(string.Format(@"ACOTH({0})", input.ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expectedResult, actual, tolerance * 10); + } + + [Theory] + public void Acoth_ForPlusMinusXSmallerThan1_ThrowsNumberException([Range(-0.9, 0.9, 0.1)] double input) + { + Assert.Throws(() => XLWorkbook.EvaluateExpr(string.Format(@"ACOTH({0})", input.ToString(CultureInfo.InvariantCulture)))); + } + [TestCase(4, 3, 20)] [TestCase(10, 3, 220)] [TestCase(0, 0, 1)]