56 lines
1.5 KiB
Solidity
56 lines
1.5 KiB
Solidity
pragma solidity ^0.5.2;
|
|
|
|
|
|
library SafeMath {
|
|
/**
|
|
@dev returns the sum of _x and _y, reverts if the calculation overflows
|
|
@param _x value 1
|
|
@param _y value 2
|
|
@return sum
|
|
*/
|
|
function add(uint256 _x, uint256 _y) internal pure returns (uint256) {
|
|
uint256 z = _x + _y;
|
|
require(z >= _x, "SafeMath failed");
|
|
return z;
|
|
}
|
|
|
|
/**
|
|
@dev returns the difference of _x minus _y, reverts if the calculation underflows
|
|
@param _x minuend
|
|
@param _y subtrahend
|
|
@return difference
|
|
*/
|
|
function sub(uint256 _x, uint256 _y) internal pure returns (uint256) {
|
|
require(_x >= _y, "SafeMath failed");
|
|
return _x - _y;
|
|
}
|
|
|
|
/**
|
|
@dev returns the product of multiplying _x by _y, reverts if the calculation overflows
|
|
@param _x factor 1
|
|
@param _y factor 2
|
|
@return product
|
|
*/
|
|
function mul(uint256 _x, uint256 _y) internal pure returns (uint256) {
|
|
// gas optimization
|
|
if (_x == 0)
|
|
return 0;
|
|
|
|
uint256 z = _x * _y;
|
|
require(z / _x == _y, "SafeMath failed");
|
|
return z;
|
|
}
|
|
|
|
/**
|
|
@dev Integer division of two numbers truncating the quotient, reverts on division by zero.
|
|
@param _x dividend
|
|
@param _y divisor
|
|
@return quotient
|
|
*/
|
|
function div(uint256 _x, uint256 _y) internal pure returns (uint256) {
|
|
require(_y > 0, "SafeMath failed");
|
|
uint256 c = _x / _y;
|
|
|
|
return c;
|
|
}
|
|
} |