diff --git a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs index 43fce93..e6fdb4a 100644 --- a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs +++ b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs @@ -28,6 +28,7 @@ ce.RegisterFunction("COS", 1, Cos); ce.RegisterFunction("COSH", 1, Cosh); ce.RegisterFunction("COT", 1, Cot); + ce.RegisterFunction("COTH", 1, Coth); ce.RegisterFunction("CSCH", 1, Csch); ce.RegisterFunction("DECIMAL", 2, MathTrig.Decimal); ce.RegisterFunction("DEGREES", 1, Degrees); @@ -131,6 +132,15 @@ return 1 / tan; } + private static object Coth(List p) + { + double input = p[0]; + if (input == 0) + throw new DivisionByZeroException(); + + return 1 / Math.Tanh(input); + } + private static object Csch(List p) { if (Math.Abs((double)p[0].Evaluate()) < Double.Epsilon) diff --git a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs index 8c95b10..06ac520 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs @@ -79,6 +79,38 @@ Assert.Throws(() => XLWorkbook.EvaluateExpr("COT(0)")); } + [TestCase(-10, -1.000000004)] + [TestCase(-9, -1.00000003)] + [TestCase(-8, -1.000000225)] + [TestCase(-7, -1.000001663)] + [TestCase(-6, -1.000012289)] + [TestCase(-5, -1.000090804)] + [TestCase(-4, -1.00067115)] + [TestCase(-3, -1.004969823)] + [TestCase(-2, -1.037314721)] + [TestCase(-1, -1.313035285)] + [TestCase(1, 1.313035285)] + [TestCase(2, 1.037314721)] + [TestCase(3, 1.004969823)] + [TestCase(4, 1.00067115)] + [TestCase(5, 1.000090804)] + [TestCase(6, 1.000012289)] + [TestCase(7, 1.000001663)] + [TestCase(8, 1.000000225)] + [TestCase(9, 1.00000003)] + [TestCase(10, 1.000000004)] + public void Coth_Examples(double input, double expected) + { + var actual = (double)XLWorkbook.EvaluateExpr(string.Format(@"COTH({0})", input.ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expected, actual, tolerance * 10.0); + } + + [Test] + public void Cot_On0_ThrowsDivisionByZeroException() + { + Assert.Throws(() => XLWorkbook.EvaluateExpr(@"COTH(0)")); + } + [TestCase("FF", 16, 255)] [TestCase("111", 2, 7)] [TestCase("zap", 36, 45745)]