Skip to content

Commit

Permalink
Add RealFunctions: sec, csc, asec, acsc, sech, csch, asech, acsch
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Oct 19, 2023
1 parent e0437b5 commit 30bb232
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/fintamath/numbers/RealFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Real tan(const Real &rhs);

Real cot(const Real &rhs);

Real sec(const Real &rhs);

Real csc(const Real &rhs);

Real asin(const Real &rhs);

Real acos(const Real &rhs);
Expand All @@ -36,6 +40,10 @@ Real atan(const Real &rhs);

Real acot(const Real &rhs);

Real asec(const Real &rhs);

Real acsc(const Real &rhs);

Real sinh(const Real &rhs);

Real cosh(const Real &rhs);
Expand All @@ -44,6 +52,10 @@ Real tanh(const Real &rhs);

Real coth(const Real &rhs);

Real sech(const Real &rhs);

Real csch(const Real &rhs);

Real asinh(const Real &rhs);

Real acosh(const Real &rhs);
Expand All @@ -52,6 +64,10 @@ Real atanh(const Real &rhs);

Real acoth(const Real &rhs);

Real asech(const Real &rhs);

Real acsch(const Real &rhs);

Real getE();

Real getPi();
Expand Down
67 changes: 67 additions & 0 deletions src/fintamath/numbers/RealFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ Real cot(const Real &rhs) {
}
}

Real sec(const Real &rhs) {
return 1 / cos(rhs);
}

Real csc(const Real &rhs) {
try {
return 1 / sin(rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("csc", {rhs.toString()});
}
}

Real asin(const Real &rhs) {
cpp_dec_float_100 res = asin(rhs.getBackend());

Expand Down Expand Up @@ -123,6 +136,24 @@ Real acot(const Real &rhs) {
}
}

Real asec(const Real &rhs) {
try {
return acos(1 / rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("asec", {rhs.toString()});
}
}

Real acsc(const Real &rhs) {
try {
return asin(1 / rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("acsc", {rhs.toString()});
}
}

Real sinh(const Real &rhs) {
return {sinh(rhs.getBackend())};
}
Expand All @@ -144,6 +175,24 @@ Real coth(const Real &rhs) {
}
}

Real sech(const Real &rhs) {
try {
return 1 / cosh(rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("sech", {rhs.toString()});
}
}

Real csch(const Real &rhs) {
try {
return 1 / sinh(rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("csch", {rhs.toString()});
}
}

Real asinh(const Real &rhs) {
return boost::math::asinh(rhs.getBackend());
}
Expand Down Expand Up @@ -178,6 +227,24 @@ Real acoth(const Real &rhs) {
}
}

Real asech(const Real &rhs) {
try {
return acosh(1 / rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("asech", {rhs.toString()});
}
}

Real acsch(const Real &rhs) {
try {
return asinh(1 / rhs);
}
catch (const UndefinedException &) {
throw UndefinedFunctionException("acsch", {rhs.toString()});
}
}

Real getE() {
using boost::multiprecision::default_ops::get_constant_e;
return {cpp_dec_float_100(get_constant_e<cpp_dec_float_100::backend_type>())};
Expand Down
23 changes: 23 additions & 0 deletions tests/src/numbers/RealFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ TEST(RealFunctionsTests, cotTest) {
EXPECT_THROW(cot(Real("0")), UndefinedFunctionException);
}

TEST(RealFunctionsTests, secTest) {
EXPECT_EQ(sec(Real("0")).toString(), "1.0");
EXPECT_EQ(sec(Real("1")).toString(),
"1.8508157176809256179117532413986501934703966550940092988351582778588154112615967");
EXPECT_EQ(sec(Real("1.6")).toString(),
"-34.247135610018689205295461483291306219436082882081545055042205706353264742234617");
EXPECT_EQ(sec(Real("125")).toString(),
"1.2694954638805676703376706923632363680417203139865889801435769773818587224340106");
}

TEST(RealFunctionsTests, cscTest) {
EXPECT_EQ(csc(Real("1")).toString(),
"1.1883951057781212162615994523745510035278298340979626252652536663591843673571905");
EXPECT_EQ(csc(Real("-1")).toString(),
"-1.1883951057781212162615994523745510035278298340979626252652536663591843673571905");
EXPECT_EQ(csc(Real("3.2")).toString(),
"-17.130872356878680929063816308707772644748981698069414677586948372772984323565408");
EXPECT_EQ(csc(Real("360")).toString(),
"1.0428445123825993746264366840754767090833603173462451344005105419339646525772189");

EXPECT_THROW(csc(Real("0")), UndefinedFunctionException);
}

TEST(RealFunctionsTests, asinTest) {
EXPECT_EQ(asin(Real("0")).toString(), "0.0");
EXPECT_EQ(
Expand Down

0 comments on commit 30bb232

Please sign in to comment.