aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/contracts')
-rw-r--r--packages/contracts/src/2.0.0/protocol/Exchange/libs/LibMath.sol18
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