# SafeMath

## Overview

#### License: MIT

```solidity
library SafeMath
```

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

```solidity
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256)
```

Returns the addition of two unsigned integers, with an overflow flag.

*Available since v3.4.*

### trySub

```solidity
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256)
```

Returns the substraction of two unsigned integers, with an overflow flag.

*Available since v3.4.*

### tryMul

```solidity
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256)
```

Returns the multiplication of two unsigned integers, with an overflow flag.

*Available since v3.4.*

### tryDiv

```solidity
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256)
```

Returns the division of two unsigned integers, with a division by zero flag.

*Available since v3.4.*

### tryMod

```solidity
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256)
```

Returns the remainder of dividing two unsigned integers, with a division by zero flag.

*Available since v3.4.*

### add

```solidity
function add(uint256 a, uint256 b) internal pure returns (uint256)
```

Returns the addition of two unsigned integers, reverting on overflow.

Counterpart to Solidity's `+` operator.

Requirements:

* Addition cannot overflow.

### sub

```solidity
function sub(uint256 a, uint256 b) internal pure returns (uint256)
```

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

```solidity
function mul(uint256 a, uint256 b) internal pure returns (uint256)
```

Returns the multiplication of two unsigned integers, reverting on overflow.

Counterpart to Solidity's `*` operator.

Requirements:

* Multiplication cannot overflow.

### div

```solidity
function div(uint256 a, uint256 b) internal pure returns (uint256)
```

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

```solidity
function mod(uint256 a, uint256 b) internal pure returns (uint256)
```

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

```solidity
function sub(
    uint256 a,
    uint256 b,
    string memory errorMessage
) internal pure returns (uint256)
```

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

```solidity
function div(
    uint256 a,
    uint256 b,
    string memory errorMessage
) internal pure returns (uint256)
```

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

```solidity
function mod(
    uint256 a,
    uint256 b,
    string memory errorMessage
) internal pure returns (uint256)
```

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fringe.fi/v2-smart-contract-documentation/priceoracle/priceproviders/uniswapv2/safemath.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
