diff --git a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs index ee73ea1..b042a5e 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("COT", 1, Cot); ce.RegisterFunction("CSCH", 1, Csch); ce.RegisterFunction("DECIMAL", 2, MathTrig.Decimal); ce.RegisterFunction("DEGREES", 1, Degrees); @@ -118,6 +119,16 @@ return Math.Cosh(p[0]); } + private static object Cot(List p) + { + var tan = (double)Math.Tan(p[0]); + + if (tan == 0) + throw new DivisionByZeroException(); + + return 1 / tan; + } + 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 df319ad..be8bb17 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs @@ -3,6 +3,7 @@ using ClosedXML.Excel.CalcEngine.Exceptions; using NUnit.Framework; using System; +using System.Globalization; using System.Linq; namespace ClosedXML_Tests.Excel.CalcEngine @@ -11,6 +12,33 @@ public class MathTrigTests { private readonly double tolerance = 1e-10; + + [TestCase(1, 0.642092616)] + [TestCase(2, -0.457657554)] + [TestCase(3, -7.015252551)] + [TestCase(4, 0.863691154)] + [TestCase(5, -0.295812916)] + [TestCase(6, -3.436353004)] + [TestCase(7, 1.147515422)] + [TestCase(8, -0.147065064)] + [TestCase(9, -2.210845411)] + [TestCase(10, 1.542351045)] + [TestCase(11, -0.004425741)] + [TestCase(Math.PI*0.5, 0)] + [TestCase(45, 0.617369624)] + [TestCase(-2, 0.457657554)] + [TestCase(-3, 7.015252551)] + public void Cot(double input, double expected) + { + var actual = (double)XLWorkbook.EvaluateExpr(string.Format(@"COT({0})", input.ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expected, actual, tolerance * 10.0); + } + + [Test] + public void Cot_Input0() + { + Assert.Throws(() => XLWorkbook.EvaluateExpr("COT(0)")); + } [TestCase("FF", 16, 255)] [TestCase("111", 2, 7)]