diff options
author | Remco Bloemen <remco@wicked.ventures> | 2018-08-23 03:22:58 +0800 |
---|---|---|
committer | Remco Bloemen <remco@wicked.ventures> | 2018-08-25 05:09:50 +0800 |
commit | 5d70df771b151800b8a04b5569529843701c9fbd (patch) | |
tree | e145e486ea0a4b6937c35eedd0b09e9c221bb1ed /packages/contracts | |
parent | ab5df342e1dc4add20223fab7128f9323a114b8e (diff) | |
download | dexon-sol-tools-5d70df771b151800b8a04b5569529843701c9fbd.tar dexon-sol-tools-5d70df771b151800b8a04b5569529843701c9fbd.tar.gz dexon-sol-tools-5d70df771b151800b8a04b5569529843701c9fbd.tar.bz2 dexon-sol-tools-5d70df771b151800b8a04b5569529843701c9fbd.tar.lz dexon-sol-tools-5d70df771b151800b8a04b5569529843701c9fbd.tar.xz dexon-sol-tools-5d70df771b151800b8a04b5569529843701c9fbd.tar.zst dexon-sol-tools-5d70df771b151800b8a04b5569529843701c9fbd.zip |
Add isRoundingErrorCeil
Diffstat (limited to 'packages/contracts')
-rw-r--r-- | packages/contracts/src/2.0.0/protocol/Exchange/libs/LibMath.sol | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/contracts/src/2.0.0/protocol/Exchange/libs/LibMath.sol b/packages/contracts/src/2.0.0/protocol/Exchange/libs/LibMath.sol index 3e70d1b60..c4aa2abb5 100644 --- a/packages/contracts/src/2.0.0/protocol/Exchange/libs/LibMath.sol +++ b/packages/contracts/src/2.0.0/protocol/Exchange/libs/LibMath.sol @@ -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; + } } |