SafeMath
Overview
License: MIT
Wrappers over Solidity's arithmetic operations.
NOTE: SafeMath
is generally not needed starting with Solidity 0.8, since the compiler now has built in overflow checking.
Functions info
tryAdd
Returns the addition of two unsigned integers, with an overflow flag.
Available since v3.4.
trySub
Returns the substraction of two unsigned integers, with an overflow flag.
Available since v3.4.
tryMul
Returns the multiplication of two unsigned integers, with an overflow flag.
Available since v3.4.
tryDiv
Returns the division of two unsigned integers, with a division by zero flag.
Available since v3.4.
tryMod
Returns the remainder of dividing two unsigned integers, with a division by zero flag.
Available since v3.4.
add
Returns the addition of two unsigned integers, reverting on overflow.
Counterpart to Solidity's +
operator.
Requirements:
Addition cannot overflow.
sub
Returns the subtraction of two unsigned integers, reverting on overflow (when the result is negative).
Counterpart to Solidity's -
operator.
Requirements:
Subtraction cannot overflow.
mul
Returns the multiplication of two unsigned integers, reverting on overflow.
Counterpart to Solidity's *
operator.
Requirements:
Multiplication cannot overflow.
div
Returns the integer division of two unsigned integers, reverting on division by zero. The result is rounded towards zero.
Counterpart to Solidity's /
operator.
Requirements:
The divisor cannot be zero.
mod
Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), reverting when dividing by zero.
Counterpart to Solidity's %
operator. 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.
sub
Returns the subtraction of two unsigned integers, reverting with custom message on overflow (when the result is negative).
CAUTION: This function is deprecated because it requires allocating memory for the error message unnecessarily. For custom revert reasons use {trySub}.
Counterpart to Solidity's -
operator.
Requirements:
Subtraction cannot overflow.
div
Returns the integer division of two unsigned integers, reverting with custom message 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.
mod
Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), reverting with custom message when dividing by zero.
CAUTION: This function is deprecated because it requires allocating memory for the error message unnecessarily. For custom revert reasons use {tryMod}.
Counterpart to Solidity's %
operator. 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.
Last updated