github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/xc/qtum/contracts/SafeMath.sol (about)

     1  pragma solidity ^0.4.21;
     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 c) {
    14          if (a == 0) {
    15              return 0;
    16          }
    17          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 a / b;
    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 c) {
    44          c = a + b;
    45          assert(c >= a);
    46          return c;
    47      }
    48  }