# Fixed Point Wad

A set of functions to operate over u256 numbers with 1e18 precision. It emulates the decimal precision of ERC20 to port some of their advanced math operations such as exp and exp and ln.

## Interface

### <mark style="color:blue;">wad</mark>

**It returns 1 WAD - 1\_000\_000\_000\_000\_000\_000.**&#x20;

```rust
public fun wad(): u256
```

* **@return u64**. 1e18

### <mark style="color:blue;">try\_mul\_down</mark>

**It tries to `x` \* `y` / 1\_000\_000\_000\_000\_000\_000 rounding down. It returns zero instead of throwing an overflow error.**

```rust
public fun try_mul_down(x: u256, y: u256): (bool, u256)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u256.** The result of `x` \* `y` / **1\_000\_000\_000\_000\_000\_000**.

### <mark style="color:blue;">try\_mul\_up</mark>

**It tries to `x` \* `y` / 1\_000\_000\_000\_000\_000\_000 rounding up. It returns zero instead of throwing an overflow error.**

```rust
public fun try_mul_up(x: u256, y: u256): (bool, u256)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u256.** The result of `x` \* `y` / **1\_000\_000\_000\_000\_000\_000**.

### <mark style="color:blue;">try\_div\_down</mark>

**It tries to `x` \* 1\_000\_000\_000\_000\_000\_000 / `y` rounding down. It returns zero instead of throwing an overflow error.**

```rust
public fun try_div_down(x: u256, y: u256): (bool, u256)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u256.** The result of **`x` \* 1\_000\_000\_000\_000\_000\_000 / `y`**.

### <mark style="color:blue;">try\_div\_up</mark>

**It tries to `x` \* 1\_000\_000\_000\_000\_000\_000 / `y` rounding up. It returns zero instead of throwing an overflow error.**

```rust
public fun try_div_up(x: u256, y: u256): (bool, u256)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u256.** The result of **`x` \* 1\_000\_000\_000\_000\_000\_000 / `y`**.

### <mark style="color:blue;">mul\_down</mark>

`x` \* `y` / **1\_000\_000\_000\_000\_000\_000** rounding down.

```rust
public fun mul_down(x: u256, y: u256): u256
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The result of `x` \* `y` / **1\_000\_000\_000\_000\_000\_000**.

**Aborts**

* On overflow. If the result is over the maximum u256 number.

### <mark style="color:blue;">mul\_up</mark>

`x` \* `y` / **1\_000\_000\_000\_000\_000\_000** rounding up.

```rust
public fun mul_up(x: u256, y: u256): u256
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The result of `x` \* `y` / **1\_000\_000\_000\_000\_000\_000**.

**Aborts**

* On overflow. If the result is over the maximum u256 number.

### <mark style="color:blue;">div\_down</mark>

`x` \*  **1\_000\_000\_000\_000\_000\_000** / `y` rounding down.

```rust
public fun div_down(x: u256, y: u256): u256
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The result of `x` \*  **1\_000\_000\_000\_000\_000\_000** / `y`.

**Aborts**

* On zero division.

### <mark style="color:blue;">div\_up</mark>

`x` \*  **1\_000\_000\_000\_000\_000\_000** / `y` rounding up.

```rust
public fun div_up(x: u256, y: u256): u256
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The result of `x` \*  **1\_000\_000\_000\_000\_000\_000** / `y`.

**Aborts**

* On zero division.

### <mark style="color:blue;">to\_wad</mark>

**It converts `x` precision to a , a number with a precision of 1e18.**

```rust
public fun to_wad(x: u256, decimal_factor: u256): u256
```

* **@param x:** The value to be converted.
* **@param decimal\_factor:** The current decimal scalar of x.&#x20;
* **@return u64.** The result of `x` \*  **1\_000\_000\_000\_000\_000\_000** / `y`.

**Aborts**

* decimal\_factor is zero.

### <mark style="color:blue;">exp</mark>

**It calculates e^x.**&#x20;

{% hint style="success" %}
All credits to Remco Bloemen and more information here: <https://xn--2-umb.com/22/exp-ln/>
{% endhint %}

```rust
public fun exp(x: Int): Int
```

* **@param x:** The exponent.
* **@return Int.** The result of e^x.

**Aborts**

* `x` is larger than 135305999368893231589.

### <mark style="color:blue;">ln</mark>

**It calculates ln(x).**&#x20;

{% hint style="success" %}
All credits to Remco Bloemen and more information here: <https://xn--2-umb.com/22/exp-ln/>
{% endhint %}

```rust
public fun ln(x: Int): Int
```

* **@param x:** The operand.
* **@return Int.** The result of ln(x).

**Aborts**

* `x` is negative or zero.
