SafeMath

Title: SafeMath
Description: Zeppelin Solidity Library for Math operations with safety checks throws on error.
Author: Smart Contract Solutions, Inc.
Solidity Version: ^0.4.18
Relative Path: ./contracts/supporting/SafeMath.sol
License: MIT License
Current Version: 1.4.0
Original Source: SafeMath Source

No modifications have been made to this Solidity Library from the original source.

1. Imports & Dependencies

There are no imports and dependencies exist for the SafeMath Solidity Library.

2. Variables

There are no variables for the SafeMath Solidity Library.

3. Enums

There are no enums for the SafeMath Solidity Library.

4. Events

There are no events for the SafeMath Solidity Library.

5. Mappings

There are no mappings for the SafeMath Solidity Library.

6. Modifiers

There are no modifiers for the SafeMath Solidity Library.

7. Functions

The following functions exist for the SafeMath Smart Contract:

Name Description
mul Function to safely multiply two numbers
div Function to safely divide one number from another
sub Function to safely subtract one number from another
add Function to safely add two numbers

mul

Function Name: mul
Description: Function to safely multiply two numbers
Function Type: Pure
Function Visibility: Internal
Function Modifiers: None
Return Type: uin256
Return Details: Returns the result of the multiplication

Code

The code for the mul function is as follows:

mul 1.4.0 Code
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
        return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
}

The mul function performs the following:

  • if the a argument is zero, if it is returns zero
  • Multiply a argument by b argument
  • Checks that the result divided by a argument equals the b argument. If not, it will throw
  • Return the result

Usage

The mul function has the following usage syntax:

mul Usage Example
mul(2,2);

div

Function Name: div
Description: Function to safely divide one number from another
Function Type: Pure
Function Visibility: Internal
Function Modifiers: None
Return Type: uin256
Return Details: Returns the result of the division

Code

The code for the div function is as follows:

div 1.4.0 Code
function div(uint256 a, uint256 b) internal pure returns (uint256) {

    uint256 c = a / b;

    return c;
}

The div function performs the following:

  • Divide a argument by b argument
  • Return the result

Usage

The div function has the following usage syntax:

div Usage Example
div(2,2);

sub

Function Name: sub
Description: Function to safely subtract one number from another
Function Type: Pure
Function Visibility: Internal
Function Modifiers: None
Return Type: uin256
Return Details: Returns the result of the subtraction

Code

The code for the sub function is as follows:

sub 1.4.0 Code
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
}

The sub function performs the following:

  • Checks the b argument is equal to or less than the a argument. If not, it will throw
  • Calculate and result the a argument minus the b argument

Usage

The sub function has the following usage syntax:

sub Usage Example
sub(2,1);

add

Function Name: add
Description: Function to safely add two numbers
Function Type: Pure
Function Visibility: Internal
Function Modifiers: None
Return Type: uin256
Return Details: Returns the result of the addition

Code

The code for the add function is as follows:

add 1.4.0 Code
function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
}

The add function performs the following:

  • Adds a argument to b argument
  • Checks that the result is greater than the a argument. If not, it will throw.
  • Returns the result

Usage

The add function has the following usage syntax:

add Usage Example
 add(2,2);