diff --git a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs index 43fce93..075e4d3 100644 --- a/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs +++ b/ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs @@ -17,6 +17,8 @@ ce.RegisterFunction("ABS", 1, Abs); 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); @@ -28,6 +30,8 @@ ce.RegisterFunction("COS", 1, Cos); ce.RegisterFunction("COSH", 1, Cosh); ce.RegisterFunction("COT", 1, Cot); + ce.RegisterFunction("COTH", 1, Coth); + ce.RegisterFunction("CSC", 1, Csc); ce.RegisterFunction("CSCH", 1, Csch); ce.RegisterFunction("DECIMAL", 2, MathTrig.Decimal); ce.RegisterFunction("DEGREES", 1, Degrees); @@ -61,6 +65,8 @@ ce.RegisterFunction("ROUND", 2, Round); ce.RegisterFunction("ROUNDDOWN", 2, RoundDown); ce.RegisterFunction("ROUNDUP", 1, 2, RoundUp); + ce.RegisterFunction("SEC", 1, Sec); + ce.RegisterFunction("SECH", 1, Sech); ce.RegisterFunction("SERIESSUM", 4, SeriesSum); ce.RegisterFunction("SIGN", 1, Sign); ce.RegisterFunction("SIN", 1, Sin); @@ -131,6 +137,24 @@ 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 Csc(List p) + { + double input = p[0]; + if (input == 0) + throw new DivisionByZeroException(); + + return 1 / Math.Sin(input); + } + private static object Csch(List p) { if (Math.Abs((double)p[0].Evaluate()) < Double.Epsilon) @@ -463,6 +487,27 @@ return XLMath.ACosh(p[0]); } + private static object Acot(List p) + { + double x = Math.Atan(1.0 / p[0]); + + // Acot in Excel calculates the modulus of the function above. + // as the % operator is not the modulus, but the remainder, we have to calculate the modulus by hand: + while (x < 0) + x = x + Math.PI; + + 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]); @@ -699,6 +744,20 @@ return Math.Floor(value * Math.Pow(10, digits)) / Math.Pow(10, digits); } + private static object Sec(List p) + { + double number; + if (double.TryParse(p[0], out number)) + return 1.0 / Math.Cos(number); + else + throw new CellValueException(); + } + + private static object Sech(List p) + { + return 1.0 / Math.Cosh(p[0]); + } + private static object SeriesSum(List p) { var x = (Double)p[0]; diff --git a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs index 8c95b10..5d258b4 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs @@ -12,6 +12,63 @@ { private readonly double tolerance = 1e-10; + [TestCase(-10, 3.041924001)] + [TestCase(-9, 3.030935432)] + [TestCase(-8, 3.017237659)] + [TestCase(-7, 2.999695599)] + [TestCase(-6, 2.976443976)] + [TestCase(-5, 2.944197094)] + [TestCase(-4, 2.89661399)] + [TestCase(-3, 2.819842099)] + [TestCase(-2, 2.677945045)] + [TestCase(-1, 2.35619449)] + [TestCase(0, 1.570796327)] + [TestCase(1, 0.785398163)] + [TestCase(2, 0.463647609)] + [TestCase(3, 0.321750554)] + [TestCase(4, 0.244978663)] + [TestCase(5, 0.19739556)] + [TestCase(6, 0.165148677)] + [TestCase(7, 0.141897055)] + [TestCase(8, 0.124354995)] + [TestCase(9, 0.110657221)] + [TestCase(10, 0.099668652)] + public void Acot_ReturnsCorrectValue(double input, double expectedResult) + { + var actual = (double)XLWorkbook.EvaluateExpr(string.Format(@"ACOT({0})", input.ToString(CultureInfo.InvariantCulture))); + 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)] @@ -79,6 +136,71 @@ 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(-10, 1.838163961)] + [TestCase(-9, -2.426486644)] + [TestCase(-8, -1.010756218)] + [TestCase(-7, -1.522101063)] + [TestCase(-6, 3.578899547)] + [TestCase(-5, 1.042835213)] + [TestCase(-4, 1.321348709)] + [TestCase(-3, -7.086167396)] + [TestCase(-2, -1.09975017)] + [TestCase(-1, -1.188395106)] + [TestCase(1, 1.188395106)] + [TestCase(2, 1.09975017)] + [TestCase(3, 7.086167396)] + [TestCase(4, -1.321348709)] + [TestCase(5, -1.042835213)] + [TestCase(6, -3.578899547)] + [TestCase(7, 1.522101063)] + [TestCase(8, 1.010756218)] + [TestCase(9, 2.426486644)] + [TestCase(10, -1.838163961)] + public void Csc_ReturnsCorrectValues(double input, double expected) + { + var actual = (double)XLWorkbook.EvaluateExpr(string.Format(@"CSC({0})", input.ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expected, actual, tolerance * 10); + } + + [Test] + public void Csc_On0_ThrowsDivisionByZeroException() + { + Assert.Throws(() => XLWorkbook.EvaluateExpr(@"CSC(0)")); + } + + [TestCase("FF", 16, 255)] [TestCase("111", 2, 7)] [TestCase("zap", 36, 45745)] @@ -217,6 +339,97 @@ Assert.AreEqual(0.7, actual, tolerance); } + [TestCase(0, 1)] + [TestCase(0.3, 1.0467516)] + [TestCase(0.6, 1.21162831)] + [TestCase(0.9, 1.60872581)] + [TestCase(1.2, 2.759703601)] + [TestCase(1.5, 14.1368329)] + [TestCase(1.8, -4.401367872)] + [TestCase(2.1, -1.980801656)] + [TestCase(2.4, -1.356127641)] + [TestCase(2.7, -1.10610642)] + [TestCase(3.0, -1.010108666)] + [TestCase(3.3, -1.012678974)] + [TestCase(3.6, -1.115127532)] + [TestCase(3.9, -1.377538917)] + [TestCase(4.2, -2.039730601)] + [TestCase(4.5, -4.743927548)] + [TestCase(4.8, 11.42870421)] + [TestCase(5.1, 2.645658426)] + [TestCase(5.4, 1.575565187)] + [TestCase(5.7, 1.198016873)] + [TestCase(6.0, 1.041481927)] + [TestCase(6.3, 1.000141384)] + [TestCase(6.6, 1.052373922)] + [TestCase(6.9, 1.225903187)] + [TestCase(7.2, 1.643787029)] + [TestCase(7.5, 2.884876262)] + [TestCase(7.8, 18.53381902)] + [TestCase(8.1, -4.106031636)] + [TestCase(8.4, -1.925711244)] + [TestCase(8.7, -1.335743646)] + [TestCase(9.0, -1.097537906)] + [TestCase(9.3, -1.007835594)] + [TestCase(9.6, -1.015550252)] + [TestCase(9.9, -1.124617578)] + [TestCase(10.2, -1.400039323)] + [TestCase(10.5, -2.102886109)] + [TestCase(10.8, -5.145888341)] + [TestCase(11.1, 9.593612018)] + [TestCase(11.4, 2.541355049)] + [TestCase(45, 1.90359)] + [TestCase(30, 6.48292)] + public void Sec_ReturnsCorrectNumber(double input, double expectedOutput) + { + double result = (double)XLWorkbook.EvaluateExpr( + string.Format( + @"SEC({0})", + input.ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expectedOutput, result, 0.00001); + + // as the secant is symmetric for positive and negative numbers, let's assert twice: + double resultForNegative = (double)XLWorkbook.EvaluateExpr( + string.Format( + @"SEC({0})", + (-input).ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expectedOutput, resultForNegative, 0.00001); + } + + [Test] + public void Sec_ThrowsCellValueExceptionOnNonNumericValue() + { + Assert.Throws(() => XLWorkbook.EvaluateExpr( + string.Format( + @"SEC(number)"))); + } + + [TestCase(-9, 0.00024682)] + [TestCase(-8, 0.000670925)] + [TestCase(-7, 0.001823762)] + [TestCase(-6, 0.004957474)] + [TestCase(-5, 0.013475282)] + [TestCase(-4, 0.036618993)] + [TestCase(-3, 0.099327927)] + [TestCase(-2, 0.265802229)] + [TestCase(-1, 0.648054274)] + [TestCase(0, 1)] + public void Sech_ReturnsCorrectNumber(double input, double expectedOutput) + { + double result = (double)XLWorkbook.EvaluateExpr( + string.Format( + @"SECH({0})", + input.ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expectedOutput, result, 0.00001); + + // as the secant is symmetric for positive and negative numbers, let's assert twice: + double resultForNegative = (double)XLWorkbook.EvaluateExpr( + string.Format( + @"SECH({0})", + (-input).ToString(CultureInfo.InvariantCulture))); + Assert.AreEqual(expectedOutput, resultForNegative, 0.00001); + } + /// /// refers to Example 1 from the Excel documentation, ///