Skip to content

Commit 94fe010

Browse files
committed
Added tests.
1 parent 7db764d commit 94fe010

File tree

4 files changed

+105
-8
lines changed

4 files changed

+105
-8
lines changed

include/fn/basicArithm.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,28 @@ namespace steppable::__internals::arithmetic
304304
*/
305305
std::string cot(const std::string& x, int decimals, int mode = 0);
306306

307+
/**
308+
* @brief Calculates the arc cosine of a number.
309+
*
310+
* @param x The number to calculate the arc cosine of.
311+
* @param decimals The number of decimal places to round off to.
312+
* @param mode The mode to calculate the arc cosine in. 0 = radians (default), 1 = degrees, 2 = gradians.
313+
*
314+
* @return The arc cosine of the number.
315+
*/
316+
std::string acos(const std::string& x, int decimals, int mode = 0);
317+
318+
/**
319+
* @brief Calculates the arc sine of a number.
320+
*
321+
* @param x The number to calculate the arc sine of.
322+
* @param decimals The number of decimal places to round off to.
323+
* @param mode The mode to calculate the arc sine in. 0 = radians (default), 1 = degrees, 2 = gradians.
324+
*
325+
* @return The arc sine of the number.
326+
*/
327+
std::string asin(const std::string& x, int decimals, int mode = 0);
328+
307329
/**
308330
* @brief Calculates the arc tangent of a number.
309331
*
@@ -315,6 +337,39 @@ namespace steppable::__internals::arithmetic
315337
*/
316338
std::string atan(const std::string& x, int decimals, int mode = 0);
317339

340+
/**
341+
* @brief Calculates the arc secant of a number.
342+
*
343+
* @param x The number to calculate the arc secant of.
344+
* @param decimals The number of decimal places to round off to.
345+
* @param mode The mode to calculate the arc secant in. 0 = radians (default), 1 = degrees, 2 = gradians.
346+
*
347+
* @return The arc secant of the number.
348+
*/
349+
std::string asec(const std::string& x, int decimals, int mode = 0);
350+
351+
/**
352+
* @brief Calculates the arc cosecant of a number.
353+
*
354+
* @param x The number to calculate the arc cosecant of.
355+
* @param decimals The number of decimal places to round off to.
356+
* @param mode The mode to calculate the arc cosecant in. 0 = radians (default), 1 = degrees, 2 = gradians.
357+
*
358+
* @return The arc cosecant of the number.
359+
*/
360+
std::string acsc(const std::string& x, int decimals, int mode = 0);
361+
362+
/**
363+
* @brief Calculates the arc cotangent of a number.
364+
*
365+
* @param x The number to calculate the arc cotangent of.
366+
* @param decimals The number of decimal places to round off to.
367+
* @param mode The mode to calculate the arc cotangent in. 0 = radians (default), 1 = degrees, 2 = gradians.
368+
*
369+
* @return The arc cotangent of the number.
370+
*/
371+
std::string acot(const std::string& x, int decimals, int mode = 0);
372+
318373
/**
319374
* @brief Executes a given predicate function a specified number of times.
320375
*

src/root/root.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ namespace steppable::__internals::arithmetic
171171

172172
std::string root(const std::string& _number, const std::string& base, const size_t _decimals)
173173
{
174+
if (isZeroString(_number))
175+
return "0";
174176
if (isInteger(_number))
175177
{
176178
auto result = rootSurd(_number, base);

src/trig/trig.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,22 @@ namespace steppable::__internals::arithmetic
325325

326326
std::string asin(const std::string& x, const int decimals, const int mode)
327327
{
328-
if (compare(abs(x, 0), "1", 0) != "0")
328+
if (compare(abs(x, 0), "1", 0) == "1")
329+
{
329330
error("trig::asin"s, "Arc sine is not defined here."s);
331+
return "Infinity";
332+
}
333+
if (compare(abs(x, 0), "0", 0) == "2")
334+
return "0";
330335

331336
auto x2 = power(x, "2", 0);
332337
auto x3 = multiply(x2, x, 0);
333338
auto x5 = multiply(x3, x2, 0);
334339
auto x7 = multiply(x5, x2, 0);
340+
std::string onePlusX;
341+
std::string oneMinusX;
342+
std::string sqrtOnePlusX;
343+
std::string sqrtOneMinusX;
335344
std::string result;
336345

337346
// For x <= 0.5, use Taylor series.
@@ -344,7 +353,7 @@ namespace steppable::__internals::arithmetic
344353
result = add(x, divide(x3, "6", 0), 0);
345354
result = add(result, divide(multiply(x5, "3", 0), "40", 0), 0);
346355
result = add(result, divide(multiply(x7, "5", 0), "112", 0), 0);
347-
return roundOff(result, decimals);
356+
goto out; // NOLINT(cppcoreguidelines-avoid-goto)
348357
}
349358

350359
// Othman, S. B.; Bagul, Y. J. An Innovative Method for Approximating Arcsine Function. Preprints 2022,
@@ -356,16 +365,17 @@ namespace steppable::__internals::arithmetic
356365
// /-----| /-----| 5 3
357366
// = 1.021548218 * (\/ 1 + x − \/ 1 − x ) + (0.239x − 0.138x + 0.005x)
358367

359-
auto onePlusX = add("1", x, 0);
360-
auto oneMinusX = subtract("1", x, 0);
361-
auto sqrtOnePlusX = root(onePlusX, "2", static_cast<long>(decimals) * 2);
362-
auto sqrtOneMinusX = root(oneMinusX, "2", static_cast<long>(decimals) * 2);
368+
onePlusX = add("1", x, 0);
369+
oneMinusX = subtract("1", x, 0);
370+
sqrtOnePlusX = root(onePlusX, "2", static_cast<long>(decimals) * 2);
371+
sqrtOneMinusX = root(oneMinusX, "2", static_cast<long>(decimals) * 2);
363372

364373
result = multiply("1.021548218", subtract(sqrtOnePlusX, sqrtOneMinusX, 0), 0);
365374
result = add(result,
366375
add(multiply("0.239", x5, 0), subtract(multiply("0.138", x3, 0), multiply("0.005", x, 0), 0), 0),
367376
0);
368377

378+
out:
369379
switch (mode)
370380
{
371381
case 1:
@@ -378,7 +388,7 @@ namespace steppable::__internals::arithmetic
378388
break;
379389
}
380390

381-
return result;
391+
return roundOff(result, decimals);
382392
}
383393

384394
std::string acos(const std::string& x, const int decimals, const int mode)
@@ -404,8 +414,11 @@ namespace steppable::__internals::arithmetic
404414

405415
std::string asec(const std::string& x, const int decimals, const int mode)
406416
{
407-
if (compare(abs(x, 0), "1", 0) != "0")
417+
if (compare(abs(x, 0), "1", 0) == "1")
418+
{
408419
error("trig::asec"s, "Arc secant is not defined here."s);
420+
return "Infinity";
421+
}
409422

410423
// 1
411424
// asec(x) = acos(---)
@@ -416,7 +429,10 @@ namespace steppable::__internals::arithmetic
416429
std::string acsc(const std::string& x, const int decimals, const int mode)
417430
{
418431
if (compare(abs(x, 0), "1", 0) != "0")
432+
{
419433
error("trig::acsc"s, "Arc cosecant is not defined here."s);
434+
return "Infinity";
435+
}
420436

421437
// 1
422438
// acsc(x) = asin(---)
@@ -427,7 +443,10 @@ namespace steppable::__internals::arithmetic
427443
std::string acot(const std::string& x, const int decimals, const int mode)
428444
{
429445
if (compare(abs(x, 0), "1", 0) != "0")
446+
{
430447
error("trig::acot"s, "Arc cotangent is not defined here."s);
448+
return "Infinity";
449+
}
431450

432451
// 1
433452
// acot(x) = atan(---)

tests/testTrig.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
**************************************************************************************************/
2222

2323
#include "colors.hpp"
24+
#include "fn/basicArithm.hpp"
2425
#include "output.hpp"
2526
#include "testing.hpp"
2627
#include "util.hpp"
@@ -29,4 +30,24 @@
2930
#include <iostream>
3031

3132
TEST_START()
33+
using namespace steppable::__internals::arithmetic;
34+
35+
// We can just test the basics of the trigonometric functions, as the rest are based on them.
36+
SECTION(Test sine and cosine)
37+
_.assertIsEqual(sin("30", 2, 1), "0.50");
38+
_.assertIsEqual(cos("60", 2, 1), "0.50");
39+
SECTION_END()
40+
41+
SECTION(Test tangent)
42+
_.assertIsEqual(tan("45", 2, 1), "1.00");
43+
// Zero check test
44+
_.assertIsEqual(tan("90", 2, 1), "Infinity");
45+
SECTION_END()
46+
47+
SECTION(Test arc sine)
48+
_.assertIsEqual(asin("0.5", 2, 1), "30.00");
49+
// Zero check test
50+
_.assertIsEqual(asin("0", 2, 1), "0");
51+
SECTION_END()
52+
3253
TEST_END()

0 commit comments

Comments
 (0)