Math.sol 835 B

12345678910111213141516171819202122232425262728293031
  1. pragma solidity ^0.5.0;
  2. /**
  3. * @title Math
  4. * @dev Assorted math operations.
  5. */
  6. library Math {
  7. /**
  8. * @dev Returns the largest of two numbers.
  9. */
  10. function max(uint256 a, uint256 b) internal pure returns (uint256) {
  11. return a >= b ? a : b;
  12. }
  13. /**
  14. * @dev Returns the smallest of two numbers.
  15. */
  16. function min(uint256 a, uint256 b) internal pure returns (uint256) {
  17. return a < b ? a : b;
  18. }
  19. /**
  20. * @dev Calculates the average of two numbers. Since these are integers,
  21. * averages of an even and odd number cannot be represented, and will be
  22. * rounded down.
  23. */
  24. function average(uint256 a, uint256 b) internal pure returns (uint256) {
  25. // (a + b) / 2 can overflow, so we distribute
  26. return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
  27. }
  28. }