Add ceiling division operation to the Math.sol library (#2681)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
committed by
GitHub
parent
5f7eda1f98
commit
7c754d0665
@ -28,4 +28,15 @@ library Math {
|
||||
// (a + b) / 2 can overflow, so we distribute.
|
||||
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the ceiling of the division of two numbers.
|
||||
*
|
||||
* This differs from standard division with `/` in that it rounds up instead
|
||||
* of rounding down.
|
||||
*/
|
||||
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
// (a + b - 1) / b can overflow on addition, so we distribute.
|
||||
return a / b + (a % b == 0 ? 0 : 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user