Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // File: zeppelin-solidity/contracts/math/SafeMath.sol
  2.  
  3. /**
  4.  * @title SafeMath
  5.  * @dev Math operations with safety checks that throw on error
  6.  */
  7. library SafeMath {
  8.  
  9.   /**
  10.   * @dev Multiplies two numbers, throws on overflow.
  11.   */
  12.   function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
  13.     // Gas optimization: this is cheaper than asserting 'a' not being zero, but the
  14.     // benefit is lost if 'b' is also tested.
  15.     // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
  16.     if (_a == 0) {
  17.       return 0;
  18.     }
  19.  
  20.     c = _a * _b;
  21.     assert(c / _a == _b);
  22.     return c;
  23.   }
  24.  
  25.   /**
  26.   * @dev Integer division of two numbers, truncating the quotient.
  27.   */
  28.   function div(uint256 _a, uint256 _b) internal pure returns (uint256) {
  29.     // assert(_b > 0); // Solidity automatically throws when dividing by 0
  30.     // uint256 c = _a / _b;
  31.     // assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold
  32.     return _a / _b;
  33.   }
  34.  
  35.   /**
  36.   * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  37.   */
  38.   function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
  39.     assert(_b <= _a);
  40.     return _a - _b;
  41.   }
  42.  
  43.   /**
  44.   * @dev Adds two numbers, throws on overflow.
  45.   */
  46.   function add(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
  47.     c = _a + _b;
  48.     assert(c >= _a);
  49.     return c;
  50.   }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement