mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-21 11:35:53 +00:00
Move mulDiv and lowestTerms to STAmount.cpp
This commit is contained in:
@@ -1257,4 +1257,24 @@ divRound (STAmount const& num, STAmount const& den,
|
||||
return STAmount (issue, amount, offset, resultNegative);
|
||||
}
|
||||
|
||||
// compute (value)*(mul)/(div) - avoid overflow but keep precision
|
||||
std::uint64_t
|
||||
mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
|
||||
{
|
||||
lowestTerms(value, div);
|
||||
lowestTerms(mul, div);
|
||||
|
||||
if (value < mul)
|
||||
std::swap(value, mul);
|
||||
const auto max = std::numeric_limits<std::uint64_t>::max();
|
||||
if (value > max / mul)
|
||||
{
|
||||
value /= div;
|
||||
if (value > max / mul)
|
||||
throw std::overflow_error("mulDiv");
|
||||
return value * mul;
|
||||
}
|
||||
return value * mul / div;
|
||||
}
|
||||
|
||||
} // ripple
|
||||
|
||||
Reference in New Issue
Block a user