Change behavior of ceilDiv(0, 0) and improve test coverage (#4348)

This commit is contained in:
Ernesto García
2023-06-14 14:21:42 -06:00
committed by GitHub
parent ac5480e7ca
commit 2477534260
10 changed files with 178 additions and 2 deletions

View File

@ -114,6 +114,11 @@ library Math {
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
if (b == 0) {
// Guarantee the same behavior as in a regular Solidity division.
return a / b;
}
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}