# WadRayMath

[Git Source](https://github.com/Ion-Protocol/ion-protocol/blob/88cc595825f1dc2eb738fb93e172a3e8ab7a5c43/src/libraries/math/WadRayMath.sol)

This library provides mul/div\[up/down] functionality for WAD, RAY and RAD with phantom overflow protection as well as scale\[up/down] functionality for WAD, RAY and RAD.

### [Functions](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#functions" id="functions"></a>

#### [wadMulDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#wadmuldown" id="wadmuldown"></a>

Multiplies two WAD numbers and returns the result as a WAD rounding the result down.

```
function wadMulDown(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description   |
| ---- | --------- | ------------- |
| `a`  | `uint256` | Multiplicand. |
| `b`  | `uint256` | Multiplier.   |

#### [wadMulUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#wadmulup" id="wadmulup"></a>

Multiplies two WAD numbers and returns the result as a WAD rounding the result up.

```
function wadMulUp(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description   |
| ---- | --------- | ------------- |
| `a`  | `uint256` | Multiplicand. |
| `b`  | `uint256` | Multiplier.   |

#### [wadDivDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#waddivdown" id="waddivdown"></a>

Divides two WAD numbers and returns the result as a WAD rounding the result down.

```
function wadDivDown(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description |
| ---- | --------- | ----------- |
| `a`  | `uint256` | Dividend.   |
| `b`  | `uint256` | Divisor.    |

#### [wadDivUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#waddivup" id="waddivup"></a>

Divides two WAD numbers and returns the result as a WAD rounding the result up.

```
function wadDivUp(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description |
| ---- | --------- | ----------- |
| `a`  | `uint256` | Dividend.   |
| `b`  | `uint256` | Divisor.    |

#### [rayMulDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#raymuldown" id="raymuldown"></a>

Multiplies two RAY numbers and returns the result as a RAY rounding the result down.

```
function rayMulDown(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description  |
| ---- | --------- | ------------ |
| `a`  | `uint256` | Multiplicand |
| `b`  | `uint256` | Multiplier   |

#### [rayMulUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#raymulup" id="raymulup"></a>

Multiplies two RAY numbers and returns the result as a RAY rounding the result up.

```
function rayMulUp(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description  |
| ---- | --------- | ------------ |
| `a`  | `uint256` | Multiplicand |
| `b`  | `uint256` | Multiplier   |

#### [rayDivDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#raydivdown" id="raydivdown"></a>

Divides two RAY numbers and returns the result as a RAY rounding the result down.

```
function rayDivDown(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description |
| ---- | --------- | ----------- |
| `a`  | `uint256` | Dividend    |
| `b`  | `uint256` | Divisor     |

#### [rayDivUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#raydivup" id="raydivup"></a>

Divides two RAY numbers and returns the result as a RAY rounding the result up.

```
function rayDivUp(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description |
| ---- | --------- | ----------- |
| `a`  | `uint256` | Dividend    |
| `b`  | `uint256` | Divisor     |

#### [radMulDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#radmuldown" id="radmuldown"></a>

Multiplies two RAD numbers and returns the result as a RAD rounding the result down.

```
function radMulDown(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description  |
| ---- | --------- | ------------ |
| `a`  | `uint256` | Multiplicand |
| `b`  | `uint256` | Multiplier   |

#### [radMulUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#radmulup" id="radmulup"></a>

Multiplies two RAD numbers and returns the result as a RAD rounding the result up.

```
function radMulUp(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description  |
| ---- | --------- | ------------ |
| `a`  | `uint256` | Multiplicand |
| `b`  | `uint256` | Multiplier   |

#### [radDivDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#raddivdown" id="raddivdown"></a>

Divides two RAD numbers and returns the result as a RAD rounding the result down.

```
function radDivDown(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description |
| ---- | --------- | ----------- |
| `a`  | `uint256` | Dividend    |
| `b`  | `uint256` | Divisor     |

#### [radDivUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#raddivup" id="raddivup"></a>

Divides two RAD numbers and returns the result as a RAD rounding the result up.

```
function radDivUp(uint256 a, uint256 b) internal pure returns (uint256);
```

**Parameters**

| Name | Type      | Description |
| ---- | --------- | ----------- |
| `a`  | `uint256` | Dividend    |
| `b`  | `uint256` | Divisor     |

#### [scaleUpToWad](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaleuptowad" id="scaleuptowad"></a>

Scales a value up from WAD. NOTE: The `scale` value must be less than 18.

```
function scaleUpToWad(uint256 value, uint256 scale) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description            |
| ------- | --------- | ---------------------- |
| `value` | `uint256` | to scale up.           |
| `scale` | `uint256` | of the returned value. |

#### [scaleUpToRay](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaleuptoray" id="scaleuptoray"></a>

Scales a value up from RAY. NOTE: The `scale` value must be less than 27.

```
function scaleUpToRay(uint256 value, uint256 scale) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description            |
| ------- | --------- | ---------------------- |
| `value` | `uint256` | to scale up.           |
| `scale` | `uint256` | of the returned value. |

#### [scaleUpToRad](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaleuptorad" id="scaleuptorad"></a>

Scales a value up from RAD. NOTE: The `scale` value must be less than 45.

```
function scaleUpToRad(uint256 value, uint256 scale) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description            |
| ------- | --------- | ---------------------- |
| `value` | `uint256` | to scale up.           |
| `scale` | `uint256` | of the returned value. |

#### [scaleDownToWad](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaledowntowad" id="scaledowntowad"></a>

Scales a value down to WAD. NOTE: The `scale` value must be greater than 18.

```
function scaleDownToWad(uint256 value, uint256 scale) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description            |
| ------- | --------- | ---------------------- |
| `value` | `uint256` | to scale down.         |
| `scale` | `uint256` | of the returned value. |

#### [scaleDownToRay](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaledowntoray" id="scaledowntoray"></a>

Scales a value down to RAY. NOTE: The `scale` value must be greater than 27.

```
function scaleDownToRay(uint256 value, uint256 scale) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description            |
| ------- | --------- | ---------------------- |
| `value` | `uint256` | to scale down.         |
| `scale` | `uint256` | of the returned value. |

#### [scaleDownToRad](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaledowntorad" id="scaledowntorad"></a>

Scales a value down to RAD. NOTE: The `scale` value must be greater than 45.

```
function scaleDownToRad(uint256 value, uint256 scale) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description            |
| ------- | --------- | ---------------------- |
| `value` | `uint256` | to scale down.         |
| `scale` | `uint256` | of the returned value. |

#### [scaleUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaleup" id="scaleup"></a>

Scales a value up from one fixed-point precision to another.

```
function scaleUp(uint256 value, uint256 from, uint256 to) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description              |
| ------- | --------- | ------------------------ |
| `value` | `uint256` | to scale up.             |
| `from`  | `uint256` | Precision to scale from. |
| `to`    | `uint256` | Precision to scale to.   |

#### [scaleDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#scaledown" id="scaledown"></a>

Scales a value down from one fixed-point precision to another.

```
function scaleDown(uint256 value, uint256 from, uint256 to) internal pure returns (uint256);
```

**Parameters**

| Name    | Type      | Description              |
| ------- | --------- | ------------------------ |
| `value` | `uint256` | to scale down.           |
| `from`  | `uint256` | Precision to scale from. |
| `to`    | `uint256` | Precision to scale to.   |

### [Errors](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#errors" id="errors"></a>

#### [NotScalingUp](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#notscalingup" id="notscalingup"></a>

```
error NotScalingUp(uint256 from, uint256 to);
```

#### [NotScalingDown](broken://pages/jm31EM4gWXTfb5CMs4fr) <a href="#notscalingdown" id="notscalingdown"></a>

```
error NotScalingDown(uint256 from, uint256 to);
```


---

# 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.ionprotocol.io/devs/smart-contract-architecture/libraries/math/wadraymath.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.
