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.
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:
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
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 |
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:
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
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:
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