diff options
author | Remco Bloemen <remco@wicked.ventures> | 2018-08-24 03:54:39 +0800 |
---|---|---|
committer | Remco Bloemen <remco@wicked.ventures> | 2018-08-25 04:45:10 +0800 |
commit | e68942ee78eb19c27a96fb0b6b8b05c83b647bcc (patch) | |
tree | 892e693e89c466497419e5b2971dac6a54c3c915 /packages | |
parent | 4159e45aff14c04f2d799ec8a7b6e7f1a4894064 (diff) | |
download | dexon-sol-tools-e68942ee78eb19c27a96fb0b6b8b05c83b647bcc.tar.gz dexon-sol-tools-e68942ee78eb19c27a96fb0b6b8b05c83b647bcc.tar.zst dexon-sol-tools-e68942ee78eb19c27a96fb0b6b8b05c83b647bcc.zip |
Handle zero case
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/src/2.0.0/protocol/Exchange/libs/LibMath.sol | 18 |
1 files changed, 15 insertions, 3 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 146bb9943..758b7ec90 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 @@ -60,14 +60,26 @@ contract LibMath is pure returns (bool isError) { + require(denominator > 0, "DIVISION_BY_ZERO"); + // The absolute rounding error is the difference between the rounded // value and the ideal value. The relative rounding error is the // absolute rounding error divided by the absolute value of the - // ideal value. We want the relative rounding error to be strictly less - // than 0.1%. - // Let's call `numerator * target % denominator` the remainder. + // ideal value. This is undefined when the ideal value is zero. + // // The ideal value is `numerator * target / denominator`. + // Let's call `numerator * target % denominator` the remainder. // The absolute error is `remainder / denominator`. + // + // When the ideal value is zero, we require the absolute error to + // be zero. Fortunately, this is always the case. The ideal value is + // zero iff `numerator == 0` and/or `target == 0`. In this case the + // remainder and absolute error are also zero. + 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`. // We want the relative error less than 1 / 1000: // remainder / numerator * denominator < 1 / 1000 |