Skip to content

Commit d39e86c

Browse files
authored
fix bug in function preciseDivCeil (#191)
Fix bug in function preciseDivCeil. Fix bug in function preciseDivCeilInt in file utils/common/mathUtils.ts Co-authored-by: Dan Liu <[email protected]>
1 parent 0e73d21 commit d39e86c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

contracts/lib/PreciseUnitMath.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ library PreciseUnitMath {
147147

148148
a = a.mul(PRECISE_UNIT_INT);
149149
int256 c = a.div(b);
150-
if (c * b != a) {
151-
c = c > 0 ? c + 1 : c - 1;
150+
151+
if (a % b != 0) {
152+
(a ^ b > 0) ? ++c : --c;
152153
}
154+
153155
return c;
154156
}
155157

utils/common/mathUtils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ export const preciseDivCeil = (a: BigNumber, b: BigNumber): BigNumber => {
3939
};
4040

4141
export const preciseDivCeilInt = (a: BigNumber, b: BigNumber): BigNumber => {
42-
if (a.eq(0) || b.eq(0)) {
43-
return ZERO;
42+
const result = a.mul(PRECISE_UNIT).div(b);
43+
if (result.mul(b).eq(a.mul(PRECISE_UNIT))) {
44+
return result;
4445
}
4546

46-
if (a.gt(0) && b.gt(0) || a.lt(0) && b.lt(0)) {
47-
return a.mul(PRECISE_UNIT).sub(1).div(b).add(1);
47+
if ((a.gt(0) && b.gt(0)) || (a.lt(0) && b.lt(0))) {
48+
return result.add(1);
4849
} else {
49-
return a.mul(PRECISE_UNIT).add(1).div(b).sub(1);
50+
return result.sub(1);
5051
}
5152
};
5253

0 commit comments

Comments
 (0)