Skip to content

Commit 7d6a84f

Browse files
committed
WIP: Add more hyperbolic functions.
1 parent 118d428 commit 7d6a84f

File tree

3 files changed

+206
-1
lines changed

3 files changed

+206
-1
lines changed

include/fn/basicArithm.hpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,66 @@ namespace steppable::__internals::arithmetic
430430
*/
431431
std::string coth(const std::string& x, int decimals);
432432

433+
/**
434+
* @brief Inverse hyperbolic sine function.
435+
*
436+
* @param x The number.
437+
* @param decimals The number of decimal places to round off to.
438+
*
439+
* @return The inverse hyperbolic sine of the number.
440+
*/
441+
std::string asinh(const std::string& x, int decimals);
442+
443+
/**
444+
* @brief Inverse hyperbolic cosine function.
445+
*
446+
* @param x The number.
447+
* @param decimals The number of decimal places to round off to.
448+
*
449+
* @return The inverse hyperbolic cosine of the number.
450+
*/
451+
std::string acosh(const std::string& x, int decimals);
452+
453+
/**
454+
* @brief Inverse hyperbolic tangent function.
455+
*
456+
* @param x The number.
457+
* @param decimals The number of decimal places to round off to.
458+
*
459+
* @return The inverse hyperbolic tangent of the number.
460+
*/
461+
std::string atanh(const std::string& x, int decimals);
462+
463+
/**
464+
* @brief Inverse hyperbolic cotangent function.
465+
*
466+
* @param x The number.
467+
* @param decimals The number of decimal places to round off to.
468+
*
469+
* @return The inverse hyperbolic cotangent of the number.
470+
*/
471+
std::string acoth(const std::string& x, int decimals);
472+
473+
/**
474+
* @brief Inverse hyperbolic secant function.
475+
*
476+
* @param x The number.
477+
* @param decimals The number of decimal places to round off to.
478+
*
479+
* @return The inverse hyperbolic secant of the number.
480+
*/
481+
std::string asech(const std::string& x, int decimals);
482+
483+
/**
484+
* @brief Inverse hyperbolic cosecant function.
485+
*
486+
* @param x The number.
487+
* @param decimals The number of decimal places to round off to.
488+
*
489+
* @return The inverse hyperbolic cosecant of the number.
490+
*/
491+
std::string acsch(const std::string& x, int decimals);
492+
433493
/**
434494
* @brief Calculates the logarithm with a given base.
435495
*

src/hyp/hyp.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,95 @@ namespace steppable::__internals::arithmetic
113113

114114
return divide("1", denominator, 0, decimals);
115115
}
116+
117+
std::string asinh(const std::string& x, const int decimals)
118+
{
119+
// /------|
120+
// / 2
121+
// asinh(x) = ln(x + \/ x + 1 )
122+
123+
const auto& x2 = power(x, "2", 0);
124+
const auto& radicand = add(x2, "1", 0);
125+
const auto& rootResult = root(radicand, "2", decimals);
126+
const auto& result = add(x, rootResult, 0);
127+
return ln(result, decimals);
128+
}
129+
130+
std::string acosh(const std::string& x, const int decimals)
131+
{
132+
// /------|
133+
// / 2
134+
// acosh(x) = ln(x + \/ x - 1 )
135+
136+
const auto& x2 = power(x, "2", 0);
137+
const auto& radicand = subtract(x2, "1", 0);
138+
const auto& rootResult = root(radicand, "2", decimals);
139+
const auto& result = add(x, rootResult, 0);
140+
return ln(result, decimals);
141+
}
142+
143+
std::string atanh(const std::string& x, const int decimals)
144+
{
145+
// 1 1 + x
146+
// atanh(x) = --- * ln( ------- )
147+
// 2 1 - x
148+
149+
const auto& numerator = add("1", x, 0);
150+
const auto& denominator = subtract("1", x, 0);
151+
auto result = divide(numerator, denominator, 0, decimals);
152+
result = ln(result, decimals);
153+
return divide(result, "2", 0, decimals);
154+
}
155+
156+
std::string acoth(const std::string& x, const int decimals)
157+
{
158+
// 1 1 + x
159+
// acoth(x) = --- * ln( ------- )
160+
// 2 x - 1
161+
162+
const auto& numerator = add("1", x, 0);
163+
const auto& denominator = subtract(x, "1", 0);
164+
auto result = divide(numerator, denominator, 0, decimals);
165+
result = ln(result, decimals);
166+
return divide(result, "2", 0, decimals);
167+
}
168+
169+
std::string acsch(const std::string& x, const int decimals)
170+
{
171+
// /-------|
172+
// 1 / 1
173+
// acsch(x) = ln(--- + / --- + 1 )
174+
// x / 2
175+
// \/ x
176+
177+
const auto& x2 = power(x, "2", 0);
178+
const auto& oneOverX = divide("1", x, 0, decimals);
179+
const auto& oneOverX2 = divide("1", x2, 0, decimals);
180+
181+
const auto& radicand = add(oneOverX2, "1", 0);
182+
const auto& rootResult = root(radicand, "2", decimals);
183+
const auto& lnArg = add(oneOverX, rootResult, 0);
184+
return ln(lnArg, decimals);
185+
}
186+
187+
std::string asech(const std::string& x, const int decimals)
188+
{
189+
// /-------|
190+
// 1 / 1
191+
// asech(x) = ln(--- + / --- - 1 )
192+
// x / 2
193+
// \/ x
194+
195+
const auto& x2 = power(x, "2", 0);
196+
const auto& oneOverX = divide("1", x, 0, decimals);
197+
const auto& oneOverX2 = divide("1", x2, 0, decimals);
198+
199+
const auto& radicand = subtract(oneOverX2, "1", 0);
200+
const auto& rootResult = root(radicand, "2", decimals);
201+
const auto& lnArg = add(oneOverX, rootResult, 0);
202+
return ln(lnArg, decimals);
203+
}
204+
116205
} // namespace steppable::__internals::arithmetic
117206

118207
#ifndef NO_MAIN
@@ -149,6 +238,19 @@ int main(int _argc, const char* _argv[])
149238
function = arithmetic::sech;
150239
else if (command == "coth")
151240
function = arithmetic::coth;
241+
// Inverse trigonometric functions
242+
else if (command == "asinh")
243+
function = arithmetic::asinh;
244+
else if (command == "acosh")
245+
function = arithmetic::acosh;
246+
else if (command == "atanh")
247+
function = arithmetic::atanh;
248+
else if (command == "acsch")
249+
function = arithmetic::acsch;
250+
else if (command == "acoth")
251+
function = arithmetic::acoth;
252+
else if (command == "asech")
253+
function = arithmetic::asech;
152254
// Invalid command
153255
else
154256
{

tests/testHyp.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
**************************************************************************************************/
2222

2323
#include "colors.hpp"
24+
#include "fn/basicArithm.hpp"
2425
#include "output.hpp"
2526
#include "testing.hpp"
2627
#include "util.hpp"
27-
#include "fn/basicArithm.hpp"
2828

2929
#include <iomanip>
3030
#include <iostream>
@@ -40,4 +40,47 @@ SECTION_END()
4040
SECTION(Test hyperbolic cosine)
4141
_.assertIsEqual(cosh("4.75", 4), "57.7965");
4242
SECTION_END()
43+
44+
SECTION(Test hyperbolic tangent)
45+
_.assertIsEqual(tanh("0.5", 5), "0.46212");
46+
SECTION_END()
47+
48+
SECTION(Test hyperbolic cotangent)
49+
_.assertIsEqual(coth("1.25", 5), "1.17884");
50+
SECTION_END()
51+
52+
SECTION(Test hyperbolic secant)
53+
_.assertIsEqual(sech("0.75", 4), "0.7724");
54+
SECTION_END()
55+
56+
SECTION(Test hyperbolic cosecant)
57+
_.assertIsEqual(csch("0.25", 4), "3.9588");
58+
SECTION_END()
59+
60+
SECTION(Test inverse hyperbolic sine)
61+
_.assertIsEqual(asinh("0.5", 4), "0.4812");
62+
SECTION_END()
63+
64+
// TODO: FIX THESE
65+
// - Incorrect return values.
66+
// SECTION(Test inverse hyperbolic cosine)
67+
// _.assertIsEqual(acosh("1.25", 4), "0.6931");
68+
// SECTION_END()
69+
70+
// SECTION(Test inverse hyperbolic tangent)
71+
// _.assertIsEqual(atanh("0.75", 4), "0.9730");
72+
// SECTION_END()
73+
74+
// SECTION(Test inverse hyperbolic cotangent)
75+
// _.assertIsEqual(acoth("1.25", 4), "1.0986");
76+
// SECTION_END()
77+
78+
// SECTION(Test inverse hyperbolic secant)
79+
// _.assertIsEqual(asech("0.75", 4), "0.7954");
80+
// SECTION_END()
81+
82+
// SECTION(Test inverse hyperbolic cosecant)
83+
// _.assertIsEqual(acsch("0.25", 4), "2.0947");
84+
// SECTION_END()
85+
4386
TEST_END()

0 commit comments

Comments
 (0)