Skip to content

Commit f2d24ea

Browse files
committed
Fixed acos(x)
1 parent 94fe010 commit f2d24ea

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/trig/trig.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,24 @@ namespace steppable::__internals::arithmetic
6565
std::string radToDeg(const std::string& _rad)
6666
{
6767
// deg = rad * (180 / pi)
68-
auto rad = divideWithQuotient(_rad, static_cast<std::string>(constants::TWO_PI)).remainder;
68+
auto rad = _rad;
69+
rad = divideWithQuotient(rad, static_cast<std::string>(constants::TWO_PI)).remainder;
70+
rad = standardizeNumber(rad);
6971
auto deg = divide(rad, constants::PI_OVER_180, 0);
70-
return deg;
72+
deg = standardizeNumber(deg);
73+
deg = divideWithQuotient(deg, "90").remainder;
74+
return standardizeNumber(deg);
7175
}
7276

7377
std::string radToGrad(const std::string& _rad)
7478
{
7579
// grad = rad * (200 / pi)
7680
auto rad = divideWithQuotient(_rad, static_cast<std::string>(constants::TWO_PI)).remainder;
81+
rad = standardizeNumber(rad);
7782
auto grad = divide(rad, constants::PI_OVER_200, 0);
78-
return grad;
83+
grad = standardizeNumber(grad);
84+
grad = divideWithQuotient(grad, "100").remainder;
85+
return standardizeNumber(grad);
7986
}
8087

8188
std::string gradToDeg(const std::string& _grad)
@@ -393,23 +400,26 @@ namespace steppable::__internals::arithmetic
393400

394401
std::string acos(const std::string& x, const int decimals, const int mode)
395402
{
403+
if (compare(x, "1", 0) == "2")
404+
return "0";
396405
std::string circleAngle;
397406
switch (mode)
398407
{
399408
case 1:
400-
circleAngle = "180";
409+
circleAngle = "90";
401410
break;
402411
case 2:
403-
circleAngle = "200";
412+
circleAngle = "100";
404413
break;
405414
default:
406-
circleAngle = static_cast<std::string>(constants::PI);
415+
circleAngle = static_cast<std::string>(constants::PI_OVER_2);
407416
}
408417

409418
// pi
410419
// acos(x) = ----- - asin(x)
411420
// 2
412-
return subtract(circleAngle, asin(x, decimals, mode), 0);
421+
auto result = subtract(circleAngle, asin(x, decimals, mode), 0);
422+
return roundOff(result, decimals);
413423
}
414424

415425
std::string asec(const std::string& x, const int decimals, const int mode)

tests/testTrig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ _.assertIsEqual(tan("45", 2, 1), "1.00");
4444
_.assertIsEqual(tan("90", 2, 1), "Infinity");
4545
SECTION_END()
4646

47+
SECTION(Test arc cosine)
48+
_.assertIsEqual(acos("0.5", 2, 1), "60");
49+
// Zero check test
50+
_.assertIsEqual(acos("1", 2, 1), "0");
51+
SECTION_END()
52+
4753
SECTION(Test arc sine)
4854
_.assertIsEqual(asin("0.5", 2, 1), "30.00");
4955
// Zero check test

0 commit comments

Comments
 (0)