# Math256

## Structs

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

```rust
const MAX_U256: u256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
```

## Interface

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

**It tries to perform `x` + `y`. Checks for overflow.**

```rust
public fun try_add(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`. If it fails, it will be 0.

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

**It tries to perform `x` - `y`. Checks for underflow.**

```rust
public fun try_sub(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`. If it fails, it will be 0.

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

**It tries to perform `x` \* `y`.**

```rust
public fun try_mul(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`. If it fails, it will be 0.

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

**It tries to perform `x` / y rounding down.**

```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 / y. If it fails, it will be 0.

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

**It tries to perform `x` / y rounding up.**

```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` / `y`. If it fails, it will be 0.

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

**It tries to perform `x` \* `y` / `z` rounding down. Checks for zero division and overflow.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@param z:** The divisor.
* **@return bool.** If the operation was successful.
* **@return u256.** The result of `x` \* `y` / `z`. If it fails, it will be 0.

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

**It tries to perform `x` \* `y` / `z` rounding up.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@param z:** The divisor.
* **@return bool.** If the operation was successful.
* **@return u256.** The result of `x` \* `y` / `z`. If it fails, it will be 0.

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

**It tries to perform `x` % `y`.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@param z:** The divisor.
* **@return bool.** If the operation was successful.
* **@return u256.** The result of `x` % `y`. If it fails, it will be 0.

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

**It performs `x` \* `y`.**

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

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

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

**It performs `x` / `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` / `y`.

**Abort**

* It will throw on zero division.

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

**It performs `x` / `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` / `y`.

**Abort**

* It will throw on zero division.

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

**It performs `x` \* `y` / `z` rounding down.**

```rust
public fun mul_div_down(x: u256, y: u256, z: u256): u256
```

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

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

**It performs `x` \* `y` / `z` rounding up.**

```rust
public fun mul_div_up(x: u256, y: u256, z: u256): u256
```

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

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

**It returns the lowest number.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The lowest number.

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

**It returns the largest number.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The largest number.

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

**Clamps `x` between the range of \[lower, upper]**

```rust
public fun clamp(x: u256, lower: u256, upper: u256): u256
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The clamped x.

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

**Performs |x - y|.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u256.** The difference.

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

**Performs n^e.**

```rust
public fun pow(n: u256, e: u256): u256
```

* **@param n:** The base.
* **@param e:** The exponent.
* **@return u256.** The result of n^e.

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

**Adds all x in `nums` in a vector.**

```rust
public fun sum(nums: vector<u256>): u256
```

* **@param nums:** A vector of numbers.
* **@return u256.** The sum.

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

**It returns the average between two numbers (`x` + `y`) / 2.**

{% hint style="success" %}
It does not overflow.
{% endhint %}

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

* **@param** x: The first operand.
* **@param y**: The second operand.
* **@return u256.** (`x` + `y`) / 2.

### <mark style="color:blue;">average\_vector</mark>

**Calculates the average of the vector of numbers sum of vector/length of vector.**

```rust
public fun average_vector(nums: vector<u256>): u256
```

* **@param nums**: A vector of numbers.
* **@return u256.** The average.

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

**Returns the square root of `x` number. If the number is not a perfect square, the x is rounded down.**

```rust
public fun sqrt_down(x: u256): u256
```

* **@param a:** The operand.
* **@return u256.** The square root of x rounding down.

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

**Returns the square root of `x` number. If the number is not a perfect square, the `x` is rounded up.**

```rust
public fun sqrt_up(x: u256): u256
```

* **@param a:** The operand.
* **@return u256.** The square root of x rounding up.

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

**Returns the log2(x) rounding down.**

```rust
public fun log2_down(x: u256): u8
```

* **@param x:** The operand.
* **@return u8.** Log2(x).

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

**Returns the log2(x) rounding up.**

```rust
public fun log2_up(x: u256): u16
```

* **@param x:** The operand.
* **@return u16.** Log2(x).

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

**Returns the log10(x) rounding down.**

```rust
public fun log10_down(x: u256): u8
```

* **@param x:** The operand.
* **@return u8.** Log10(x)

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

**Returns the log10(x) rounding up.**

```rust
public fun log10_up(x: u256): u8
```

* **@param x:** The operand.
* **@return u8.** Log10(x)

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

**Returns the log256(x) rounding down.**

```rust
public fun log256_down(x: u256): u8
```

* **@param x:** The operand.
* **@return u8.** Log256(x).

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

**Returns the log256(x) rounding up.**

```rust
public fun log256_up(x: u256): u8
```

* **@param x:** The operand.
* **@return u8.** Log256(x).
