Math.sol 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (utils/math/Math.sol)
  3. pragma solidity ^0.8.0;
  4. /**
  5. * @dev Standard math utilities missing in the Solidity language.
  6. */
  7. library Math {
  8. /**
  9. * @dev Returns the largest of two numbers.
  10. */
  11. function max(uint256 a, uint256 b) internal pure returns (uint256) {
  12. return a >= b ? a : b;
  13. }
  14. /**
  15. * @dev Returns the smallest of two numbers.
  16. */
  17. function min(uint256 a, uint256 b) internal pure returns (uint256) {
  18. return a < b ? a : b;
  19. }
  20. /**
  21. * @dev Returns the average of two numbers. The result is rounded towards
  22. * zero.
  23. */
  24. function average(uint256 a, uint256 b) internal pure returns (uint256) {
  25. // (a + b) / 2 can overflow.
  26. return (a & b) + (a ^ b) / 2;
  27. }
  28. /**
  29. * @dev Returns the ceiling of the division of two numbers.
  30. *
  31. * This differs from standard division with `/` in that it rounds up instead
  32. * of rounding down.
  33. */
  34. function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
  35. // (a + b - 1) / b can overflow on addition, so we distribute.
  36. return a / b + (a % b == 0 ? 0 : 1);
  37. }
  38. }