diff --git a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs index 2a4d6fe..ee73ea1 100644 --- a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs +++ b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs @@ -26,6 +26,7 @@ ce.RegisterFunction("COMBIN", 2, Combin); ce.RegisterFunction("COS", 1, Cos); ce.RegisterFunction("COSH", 1, Cosh); + ce.RegisterFunction("CSCH", 1, Csch); ce.RegisterFunction("DECIMAL", 2, MathTrig.Decimal); ce.RegisterFunction("DEGREES", 1, Degrees); ce.RegisterFunction("EVEN", 1, Even); @@ -117,6 +118,14 @@ return Math.Cosh(p[0]); } + private static object Csch(List p) + { + if (Math.Abs((double)p[0].Evaluate()) < Double.Epsilon) + throw new DivisionByZeroException(); + + return 1 / Math.Sinh(p[0]); + } + private static object Decimal(List p) { string source = p[0]; diff --git a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs index 8f95e4f..df319ad 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs @@ -167,5 +167,27 @@ Assert.Throws(() => ws.Evaluate("SUMPRODUCT(A1:A10, B1:B5)")); } } + + [TestCase(1, 0.850918128)] + [TestCase(2, 0.275720565)] + [TestCase(3, 0.09982157)] + [TestCase(4, 0.03664357)] + [TestCase(5, 0.013476506)] + [TestCase(6, 0.004957535)] + [TestCase(7, 0.001823765)] + [TestCase(8, 0.000670925)] + [TestCase(9, 0.00024682)] + [TestCase(10, 0.000090799859712122200000)] + [TestCase(11, 0.0000334034)] + public void CSch_CalculatesCorrectValues(double input, double expectedOutput) + { + Assert.AreEqual(expectedOutput, (double)XLWorkbook.EvaluateExpr($@"CSCH({input})"), 0.000000001); + } + + [Test] + public void Csch_ReturnsDivisionByZeroErrorOnInput0() + { + Assert.Throws(() => XLWorkbook.EvaluateExpr("CSCH(0)")); + } } }