SignedMath

Git Source

Performs signed math operations safely, reverting with a custom error in case of overflow.

Functions

safeAdd

Safely adds two signed integers, reverting on overflow.

function safeAdd(int256 lhs, int256 rhs) internal pure returns (int256 res_);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

Returns

NameTypeDescription
res_int256The result of lhs + rhs.

safeSub

Safely subtracts two signed integers, reverting on overflow.

function safeSub(int256 lhs, int256 rhs) internal pure returns (int256 res_);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

Returns

NameTypeDescription
res_int256The result of lhs - rhs.

safeMul

Safely multiplies two signed integers, reverting on overflow.

function safeMul(int256 lhs, int256 rhs) internal pure returns (int256 res_);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

Returns

NameTypeDescription
res_int256The result of lhs * rhs.

safeDiv

Safely divides two signed integers, reverting on division by zero.

function safeDiv(int256 lhs, int256 rhs) internal pure returns (int256 res_);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

Returns

NameTypeDescription
res_int256The result of lhs / rhs.

Errors

SignedMathOverflowedAdd

The signed add operation overflowed.

error SignedMathOverflowedAdd(int256 lhs, int256 rhs);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

SignedMathOverflowedSub

The signed sub operation overflowed.

error SignedMathOverflowedSub(int256 lhs, int256 rhs);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

SignedMathOverflowedMul

The signed mul operation overflowed.

error SignedMathOverflowedMul(int256 lhs, int256 rhs);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

SignedMathOverflowedDiv

The signed div operation overflowed.

error SignedMathOverflowedDiv(int256 lhs, int256 rhs);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.
rhsint256The right-hand side operand.

SignedMathDivideByZero

A division by zero occurred.

error SignedMathDivideByZero(int256 lhs);

Parameters

NameTypeDescription
lhsint256The left-hand side operand.