mirror of
https://github.com/XRPLF/rippled.git
synced 2026-08-21 14:20:56 +00:00
Switch to series expansion method for ln() (#6268)
* Switch to series expansion method for ln() Add float lg() tests to Number tests; * Rename lg -> log10 * Add check for 0 to log10()
This commit is contained in:
@@ -1712,6 +1712,89 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
test_log10()
|
||||
{
|
||||
auto const scale = Number::getMantissaScale();
|
||||
testcase << "test_lg " << to_string(scale);
|
||||
|
||||
using Case = std::tuple<Number, Number>;
|
||||
auto test = [this](auto const& c) {
|
||||
for (auto const& [x, z] : c)
|
||||
{
|
||||
auto const result = log10(x);
|
||||
std::stringstream ss;
|
||||
ss << "lg(" << x << ") = " << result << ". Expected: " << z;
|
||||
// std::cout << ss.str() << std::endl;
|
||||
BEAST_EXPECTS(result == z, ss.str());
|
||||
}
|
||||
};
|
||||
|
||||
auto const cSmall = std::to_array<Case>(
|
||||
{{Number{2}, Number{3'010'299'956'639'811ll, -16}},
|
||||
{Number{2'000'000}, Number{6'301'029'995'663'985ll, -15}},
|
||||
{Number{2, -30}, Number{-2'969'897'000'433'602ll, -14}},
|
||||
{Number{1}, Number{0}},
|
||||
{Number{1'000'000'000'000'000ll}, Number{15}},
|
||||
{Number{5625, -4}, Number{-2'498'774'732'165'998, -16}}});
|
||||
|
||||
auto const cLarge = std::to_array<Case>(
|
||||
{{Number{
|
||||
false, Number::maxMantissa() - 9, -1, Number::normalized{}},
|
||||
Number{
|
||||
false,
|
||||
1'746'901'684'478'673'451ll,
|
||||
-17,
|
||||
Number::normalized{}}},
|
||||
{Number{false, Number::maxMantissa() - 9, 0, Number::normalized{}},
|
||||
Number{
|
||||
false,
|
||||
1'846'901'684'478'673'451ll,
|
||||
-17,
|
||||
Number::normalized{}}},
|
||||
{Number{Number::maxRep},
|
||||
Number{
|
||||
false,
|
||||
1'861'728'612'932'620'011ll,
|
||||
-17,
|
||||
Number::normalized{}}}});
|
||||
|
||||
if (Number::getMantissaScale() == MantissaRange::small)
|
||||
{
|
||||
test(cSmall);
|
||||
}
|
||||
else
|
||||
{
|
||||
NumberRoundModeGuard mg(Number::towards_zero);
|
||||
test(cLarge);
|
||||
}
|
||||
|
||||
{
|
||||
bool caught = false;
|
||||
try
|
||||
{
|
||||
log10(Number{-2});
|
||||
}
|
||||
catch (std::runtime_error const&)
|
||||
{
|
||||
caught = true;
|
||||
}
|
||||
BEAST_EXPECT(caught);
|
||||
caught = false;
|
||||
|
||||
try
|
||||
{
|
||||
log10(Number());
|
||||
}
|
||||
catch (std::runtime_error const&)
|
||||
{
|
||||
caught = true;
|
||||
}
|
||||
BEAST_EXPECT(caught);
|
||||
caught = false;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
@@ -1739,6 +1822,7 @@ public:
|
||||
test_truncate();
|
||||
testRounding();
|
||||
testInt64();
|
||||
test_log10();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user