Handle zero case
This commit is contained in:
parent
4159e45aff
commit
e68942ee78
@ -60,14 +60,26 @@ contract LibMath is
|
|||||||
pure
|
pure
|
||||||
returns (bool isError)
|
returns (bool isError)
|
||||||
{
|
{
|
||||||
|
require(denominator > 0, "DIVISION_BY_ZERO");
|
||||||
|
|
||||||
// The absolute rounding error is the difference between the rounded
|
// The absolute rounding error is the difference between the rounded
|
||||||
// value and the ideal value. The relative rounding error is the
|
// value and the ideal value. The relative rounding error is the
|
||||||
// absolute rounding error divided by the absolute value of the
|
// absolute rounding error divided by the absolute value of the
|
||||||
// ideal value. We want the relative rounding error to be strictly less
|
// ideal value. This is undefined when the ideal value is zero.
|
||||||
// than 0.1%.
|
//
|
||||||
// Let's call `numerator * target % denominator` the remainder.
|
|
||||||
// The ideal value is `numerator * target / denominator`.
|
// The ideal value is `numerator * target / denominator`.
|
||||||
|
// Let's call `numerator * target % denominator` the remainder.
|
||||||
// The absolute error is `remainder / denominator`.
|
// 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`.
|
// The relative error is `remainder / numerator * target`.
|
||||||
// We want the relative error less than 1 / 1000:
|
// We want the relative error less than 1 / 1000:
|
||||||
// remainder / numerator * denominator < 1 / 1000
|
// remainder / numerator * denominator < 1 / 1000
|
||||||
|
Loading…
x
Reference in New Issue
Block a user