Skip to content

Latest commit

 

History

History
373 lines (324 loc) · 11.5 KB

SignedSafeMath.md

File metadata and controls

373 lines (324 loc) · 11.5 KB

SignedSafeMath (SignedSafeMath.sol)

View Source: contracts/openzeppelin/SignedSafeMath.sol

SignedSafeMath contract

Signed math operations with safety checks that revert on error.

Contract Members

Constants & Variables

int256 private constant _INT256_MIN;

Functions


mul

Returns the multiplication of two signed integers, reverting on overflow. * Counterpart to Solidity's * operator. * Requirements: * - Multiplication cannot overflow.

function mul(int256 a, int256 b) internal pure
returns(int256)

Arguments

Name Type Description
a int256
b int256
Source Code
function mul(int256 a, int256 b) internal pure returns (int256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) {
            return 0;
        }

        require(!(a == -1 && b == _INT256_MIN), "SignedSafeMath: multiplication overflow");

        int256 c = a * b;
        require(c / a == b, "SignedSafeMath: multiplication overflow");

        return c;
    }

div

Returns the integer division of two signed integers. Reverts on division by zero. The result is rounded towards zero. * Counterpart to Solidity's / operator. Note: this function uses a revert opcode (which leaves remaining gas untouched) while Solidity uses an invalid opcode to revert (consuming all remaining gas). * Requirements: * - The divisor cannot be zero.

function div(int256 a, int256 b) internal pure
returns(int256)

Arguments

Name Type Description
a int256
b int256
Source Code
function div(int256 a, int256 b) internal pure returns (int256) {
        require(b != 0, "SignedSafeMath: division by zero");
        require(!(b == -1 && a == _INT256_MIN), "SignedSafeMath: division overflow");

        int256 c = a / b;

        return c;
    }

sub

Returns the subtraction of two signed integers, reverting on overflow. * Counterpart to Solidity's - operator. * Requirements: * - Subtraction cannot overflow.

function sub(int256 a, int256 b) internal pure
returns(int256)

Arguments

Name Type Description
a int256
b int256
Source Code
function sub(int256 a, int256 b) internal pure returns (int256) {
        int256 c = a - b;
        require((b >= 0 && c <= a) || (b < 0 && c > a), "SignedSafeMath: subtraction overflow");

        return c;
    }

add

Returns the addition of two signed integers, reverting on overflow. * Counterpart to Solidity's + operator. * Requirements: * - Addition cannot overflow.

function add(int256 a, int256 b) internal pure
returns(int256)

Arguments

Name Type Description
a int256
b int256
Source Code
function add(int256 a, int256 b) internal pure returns (int256) {
        int256 c = a + b;
        require((b >= 0 && c >= a) || (b < 0 && c < a), "SignedSafeMath: addition overflow");

        return c;
    }

Contracts