Skip to content

Commit 118d428

Browse files
committed
Add zero check to log.
1 parent 159fa68 commit 118d428

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/log/log.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ namespace steppable::__internals::arithmetic
4747
{
4848
std::string _log(const std::string& _number, const size_t _decimals)
4949
{
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+
5057
// /-\ +--------------------------------------------+
5158
// / ! \ | WARNING: DO NOT CALL THIS METHOD DIRECTLY! |
5259
// /-----\ +--------------------------------------------+
@@ -83,11 +90,15 @@ namespace steppable::__internals::arithmetic
8390
// Common logarithms
8491
std::string logb(const std::string& _number, const std::string& _base, const size_t _decimals)
8592
{
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);
91102
auto result = divide(lnX, lnB, 0, static_cast<int>(_decimals));
92103

93104
return numUtils::roundOff(result, _decimals);

0 commit comments

Comments
 (0)