Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. pragma solidity ^0.3.6;
  2.  
  3.  
  4. /**
  5. * @title SafeMath
  6. * @dev Math operations with safety checks that throw on error
  7. */
  8. library SafeMath {
  9.  
  10. /**
  11. * @dev Multiplies two numbers, throws on overflow.
  12. */
  13. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  14. if (a == 0) {
  15. return 0;
  16. }
  17. uint256 c = a * b;
  18. assert(c / a == b);
  19. return c;
  20. }
  21.  
  22. /**
  23. * @dev Integer division of two numbers, truncating the quotient.
  24. */
  25. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  26. // assert(b > 0); // Solidity automatically throws when dividing by 0
  27. uint256 c = a / b;
  28. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  29. return c;
  30. }
  31.  
  32. /**
  33. * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  34. */
  35. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  36. assert(b <= a);
  37. return a - b;
  38. }
  39.  
  40. /**
  41. * @dev Adds two numbers, throws on overflow.
  42. */
  43. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  44. uint256 c = a + b;
  45. assert(c >= a);
  46. return c;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement