Add isRoundingErrorCeil

This commit is contained in:
Remco Bloemen
2018-08-22 12:22:58 -07:00
parent ab5df342e1
commit 5d70df771b

View File

@@ -107,6 +107,7 @@ contract LibMath is
if (target == 0 || numerator == 0) {
return false;
}
// Otherwise, we want the relative rounding error to be strictly
// less than 0.1%.
// The relative error is `remainder / numerator * target`.
@@ -120,4 +121,32 @@ contract LibMath is
isError = safeMul(1000, remainder) >= safeMul(numerator, target);
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;
}
}