SafeMath.sol 830 B

1234567891011121314151617181920212223242526272829303132333435
  1. pragma solidity ^0.4.18;
  2. /**
  3. * @title SafeMath
  4. * @dev Math operations with safety checks that throw on error
  5. */
  6. library SafeMath {
  7. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  8. if (a == 0) {
  9. return 0;
  10. }
  11. uint256 c = a * b;
  12. assert(c / a == b);
  13. return c;
  14. }
  15. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  16. // assert(b > 0); // Solidity automatically throws when dividing by 0
  17. uint256 c = a / b;
  18. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  19. return c;
  20. }
  21. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  22. assert(b <= a);
  23. return a - b;
  24. }
  25. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  26. uint256 c = a + b;
  27. assert(c >= a);
  28. return c;
  29. }
  30. }