@@ -47,6 +47,13 @@ namespace steppable::__internals::arithmetic
47
47
{
48
48
std::string _log (const std::string& _number, const size_t _decimals)
49
49
{
50
+ // Zero check
51
+ if (numUtils::isZeroString (_number))
52
+ {
53
+ output::error (" log::_log" s, " The number cannot be zero." s);
54
+ return " -Infinity" ;
55
+ }
56
+
50
57
// /-\ +--------------------------------------------+
51
58
// / ! \ | WARNING: DO NOT CALL THIS METHOD DIRECTLY! |
52
59
// /-----\ +--------------------------------------------+
@@ -83,11 +90,15 @@ namespace steppable::__internals::arithmetic
83
90
// Common logarithms
84
91
std::string logb (const std::string& _number, const std::string& _base, const size_t _decimals)
85
92
{
86
- // ln(a)
87
- // log (x) = -------
88
- // b ln(b)
89
- auto lnX = _log (_number, _decimals + 2 );
90
- auto lnB = _log (_base, _decimals + 2 );
93
+ // -ln(1/a)
94
+ // log (x) = ----------
95
+ // b -ln(1/b)
96
+
97
+ auto oneOverA = divide (" 1" , _number, 0 , static_cast <int >(_decimals + 2 ));
98
+ auto oneOverB = divide (" 1" , _base, 0 , static_cast <int >(_decimals + 2 ));
99
+
100
+ auto lnX = " -" + _log (oneOverA, _decimals + 2 );
101
+ auto lnB = " -" + _log (oneOverB, _decimals + 2 );
91
102
auto result = divide (lnX, lnB, 0 , static_cast <int >(_decimals));
92
103
93
104
return numUtils::roundOff (result, _decimals);
0 commit comments