github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/xc/eth-v1.0.3/SafeMath.sol (about)

     1  pragma solidity ^0.4.19;
     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          if (a == 0) {
    14              return 0;
    15          }
    16          c = a * b;
    17          assert(c / a == b);
    18          return c;
    19      }
    20  
    21      /**
    22      * @dev Integer division of two numbers, truncating the quotient.
    23      */
    24      function div(uint256 a, uint256 b) internal pure returns (uint256) {
    25          // assert(b > 0); // Solidity automatically throws when dividing by 0
    26          // uint256 c = a / b;
    27          // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    28          return a / b;
    29      }
    30  
    31      /**
    32      * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
    33      */
    34      function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    35          assert(b <= a);
    36          return a - b;
    37      }
    38  
    39      /**
    40      * @dev Adds two numbers, throws on overflow.
    41      */
    42      function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
    43          c = a + b;
    44          assert(c >= a);
    45          return c;
    46      }
    47  }