# SafeMath.sol

View Source: [contracts/SafeMath.sol](https://app.gitbook.com/s/-MQ0mmOovvt0G2zyJIgA/contracts/SafeMath.sol)

Wrappers over Solidity's arithmetic operations with added overflow checks.

* Arithmetic operations in Solidity wrap on overflow. This can easily result

  in bugs, because programmers usually assume that an overflow raises an

  error, which is the standard behavior in high level programming languages.

  `SafeMath` restores this intuition by reverting the transaction when an

  operation overflows.
* Using this library instead of the unchecked operations eliminates an entire

  class of bugs, so it's recommended to use it always.

### Functions

* add(uint256 a, uint256 b)
* add(uint256 a, uint256 b, string errorMessage)
* sub(uint256 a, uint256 b)
* sub(uint256 a, uint256 b, string errorMessage)
* mul(uint256 a, uint256 b)
* mul(uint256 a, uint256 b, string errorMessage)
* div(uint256 a, uint256 b)
* div(uint256 a, uint256 b, string errorMessage)
* mod(uint256 a, uint256 b)
* mod(uint256 a, uint256 b, string errorMessage)

#### add

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

* Counterpart to Solidity's `+` operator.
* Requirements:
  * Addition cannot overflow.

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

**Arguments**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| a    | uint256 |             |
| b    | uint256 |             |

#### add

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

* Counterpart to Solidity's `+` operator.
* Requirements:
  * Addition cannot overflow.

```javascript
function add(uint256 a, uint256 b, string errorMessage) internal pure
returns(uint256)
```

**Arguments**

| Name         | Type    | Description |
| ------------ | ------- | ----------- |
| a            | uint256 |             |
| b            | uint256 |             |
| errorMessage | string  |             |

#### sub

Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative).

* Counterpart to Solidity's `-` operator.
* Requirements:
  * Subtraction cannot underflow.

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

**Arguments**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| a    | uint256 |             |
| b    | uint256 |             |

#### sub

Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative).

* Counterpart to Solidity's `-` operator.
* Requirements:
  * Subtraction cannot underflow.

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

**Arguments**

| Name         | Type    | Description |
| ------------ | ------- | ----------- |
| a            | uint256 |             |
| b            | uint256 |             |
| errorMessage | string  |             |

#### mul

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

* Counterpart to Solidity's `*` operator.
* Requirements:
  * Multiplication cannot overflow.

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

**Arguments**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| a    | uint256 |             |
| b    | uint256 |             |

#### mul

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

* Counterpart to Solidity's `*` operator.
* Requirements:
  * Multiplication cannot overflow.

```javascript
function mul(uint256 a, uint256 b, string errorMessage) internal pure
returns(uint256)
```

**Arguments**

| Name         | Type    | Description |
| ------------ | ------- | ----------- |
| a            | uint256 |             |
| b            | uint256 |             |
| errorMessage | string  |             |

#### div

Returns the integer division of two unsigned 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.

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

**Arguments**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| a    | uint256 |             |
| b    | uint256 |             |

#### div

Returns the integer division of two unsigned integers. Reverts 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.

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

**Arguments**

| Name         | Type    | Description |
| ------------ | ------- | ----------- |
| a            | uint256 |             |
| b            | uint256 |             |
| errorMessage | string  |             |

#### mod

Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), Reverts 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.

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

**Arguments**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| a    | uint256 |             |
| b    | uint256 |             |

#### mod

Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), Reverts with custom message 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.

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

**Arguments**

| Name         | Type    | Description |
| ------------ | ------- | ----------- |
| a            | uint256 |             |
| b            | uint256 |             |
| errorMessage | string  |             |


---

# 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.alkemi.network/earn-contracts/docs/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.
