Add isRoundingErrorCeil
This commit is contained in:
@@ -107,6 +107,7 @@ contract LibMath is
|
|||||||
if (target == 0 || numerator == 0) {
|
if (target == 0 || numerator == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we want the relative rounding error to be strictly
|
// Otherwise, we want the relative rounding error to be strictly
|
||||||
// less than 0.1%.
|
// less than 0.1%.
|
||||||
// The relative error is `remainder / numerator * target`.
|
// The relative error is `remainder / numerator * target`.
|
||||||
@@ -120,4 +121,32 @@ contract LibMath is
|
|||||||
isError = safeMul(1000, remainder) >= safeMul(numerator, target);
|
isError = safeMul(1000, remainder) >= safeMul(numerator, target);
|
||||||
return isError;
|
return isError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @dev Checks if rounding error > 0.1%.
|
||||||
|
/// @param numerator Numerator.
|
||||||
|
/// @param denominator Denominator.
|
||||||
|
/// @param target Value to multiply with numerator/denominator.
|
||||||
|
/// @return Rounding error is present.
|
||||||
|
function isRoundingErrorCeil(
|
||||||
|
uint256 numerator,
|
||||||
|
uint256 denominator,
|
||||||
|
uint256 target
|
||||||
|
)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (bool isError)
|
||||||
|
{
|
||||||
|
require(denominator > 0, "DIVISION_BY_ZERO");
|
||||||
|
|
||||||
|
if (target == 0 || numerator == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint256 remainder = mulmod(target, numerator, denominator);
|
||||||
|
if (remainder == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
remainder = safeSub(denominator, remainder);
|
||||||
|
isError = safeMul(1000, remainder) >= safeMul(numerator, target);
|
||||||
|
return isError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user