diff --git a/src/fintamath/numbers/RealFunctions.cpp b/src/fintamath/numbers/RealFunctions.cpp index 5b3e6c3ad..369e46aa7 100644 --- a/src/fintamath/numbers/RealFunctions.cpp +++ b/src/fintamath/numbers/RealFunctions.cpp @@ -135,13 +135,12 @@ Real atan(const Real &rhs) { } Real acot(const Real &rhs) { - Real res = getPi() / 2; - - if (rhs < 0) { - res = -res; + try { + return atan(1 / rhs); + } + catch (const UndefinedException &) { + throw UndefinedFunctionException("acot", {rhs.toString()}); } - - return res - atan(rhs); } Real sinh(const Real &rhs) { @@ -166,30 +165,33 @@ Real coth(const Real &rhs) { } Real asinh(const Real &rhs) { - return ln(sqrt(rhs * rhs + 1) + rhs); + return boost::math::asinh(rhs.getBackend()); } Real acosh(const Real &rhs) { try { - return ln(rhs + sqrt(rhs - 1) * sqrt(rhs + 1)); + return boost::math::acosh(rhs.getBackend()); } - catch (const UndefinedException &) { + catch (const std::domain_error &) { throw UndefinedFunctionException("acoth", {rhs.toString()}); } } Real atanh(const Real &rhs) { try { - return (ln(1 + rhs) - ln(1 - rhs)) / 2; + return boost::math::atanh(rhs.getBackend()); } - catch (const UndefinedException &) { + catch (const std::domain_error &) { + throw UndefinedFunctionException("acoth", {rhs.toString()}); + } + catch (const std::overflow_error &) { throw UndefinedFunctionException("acoth", {rhs.toString()}); } } Real acoth(const Real &rhs) { try { - return (ln(1 + 1 / rhs) - ln(1 - 1 / rhs)) / 2; + return atanh(1 / rhs); } catch (const UndefinedException &) { throw UndefinedFunctionException("acoth", {rhs.toString()}); diff --git a/tests/src/expressions/ExpressionFunctionsTests.cpp b/tests/src/expressions/ExpressionFunctionsTests.cpp index deb1510c8..12d256256 100644 --- a/tests/src/expressions/ExpressionFunctionsTests.cpp +++ b/tests/src/expressions/ExpressionFunctionsTests.cpp @@ -187,7 +187,7 @@ TEST(ExpressionFunctionsTests, atanTest) { TEST(ExpressionFunctionsTests, acotTest) { EXPECT_EQ(acot(Expression("1")).toString(), "acot(1)"); // TODO trigonometry - EXPECT_EQ(acot(Expression("0")).toString(), "acot(0)"); // TODO trigonometry + // EXPECT_EQ(acot(Expression("0")).toString(), "acot(0)"); // TODO trigonometry EXPECT_EQ(acot(Expression("a/5")).toString(), "acot(a/5)"); } diff --git a/tests/src/functions/trigonometry/AcotTests.cpp b/tests/src/functions/trigonometry/AcotTests.cpp index 9b1bcea9e..04d105b2f 100644 --- a/tests/src/functions/trigonometry/AcotTests.cpp +++ b/tests/src/functions/trigonometry/AcotTests.cpp @@ -2,6 +2,7 @@ #include "fintamath/functions/trigonometry/Acot.hpp" +#include "fintamath/exceptions/UndefinedException.hpp" #include "fintamath/functions/arithmetic/Sub.hpp" #include "fintamath/functions/arithmetic/UnaryPlus.hpp" #include "fintamath/literals/Variable.hpp" @@ -20,8 +21,6 @@ TEST(AcotTests, getFunctionTypeTest) { } TEST(AcotTests, callTest) { - EXPECT_EQ(f(Integer(0))->toString(), - "1.5707963267948966192313216916397514420985846996875529104874722961539082031431045"); EXPECT_EQ(f(Integer(1))->toString(), "0.78539816339744830961566084581987572104929234984377645524373614807695410157155225"); EXPECT_EQ(f(Integer(10))->toString(), @@ -33,6 +32,8 @@ TEST(AcotTests, callTest) { EXPECT_EQ(f(Variable("a"))->toString(), "acot(a)"); + EXPECT_THROW(f(Integer(0)), UndefinedFunctionException); + EXPECT_THROW(f(), InvalidInputFunctionException); EXPECT_THROW(f(Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException); } diff --git a/tests/src/numbers/RealFunctionsTests.cpp b/tests/src/numbers/RealFunctionsTests.cpp index 5e099f0c7..f890aaeac 100644 --- a/tests/src/numbers/RealFunctionsTests.cpp +++ b/tests/src/numbers/RealFunctionsTests.cpp @@ -166,6 +166,8 @@ TEST(RealFunctionsTests, acotTest) { "0.78539816339744830961566084581987572104929234984377645524373614807695410157155225"); EXPECT_EQ(acot(Real("999.9996666666444444423280421164020950243150821349686957930420507767438195464464")).toString(), "0.001"); + + EXPECT_THROW(acot(Real(0)), UndefinedFunctionException); } TEST(RealFunctionsTests, sinhTest) {