# Math64

A set of functions to operate over u64 numbers.

{% hint style="warning" %}
Beware that some operations throw on overflow and underflows.
{% endhint %}

## Structs

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

```rust
const MAX_U64: u256 = 18446744073709551615;
```

## Interface

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

**It performs `x` + `y`.**

{% hint style="info" %}
It will wrap around the `MAX_U64`. `MAX_U64` + 1 = 0.
{% endhint %}

```rust
public fun wrapping_add(x: u64, y: u64): u64
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u64.** The result of `x` + `y`.

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

**It performs `x` - `y`.**

{% hint style="info" %}
It will wrap around zero. 0 - 1 = `MAX_U64`.
{% endhint %}

```rust
public fun wrapping_sub(x: u64, y: u64): u64
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u64.** The result of `x` - `y`.

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

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

{% hint style="info" %}
It will wrap around. `MAX_U64` \* `MAX_U64` = 0.
{% endhint %}

```rust
public fun wrapping_mul(x: u64, y: u64): u64
```

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

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

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

```rust
public fun try_add(x: u64, y: u64): (bool, u64)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u64.** 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: u64, y: u64): (bool, u64)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u64.** 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: u64, y: u64): (bool, u64)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u64.** 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: u64, y: u64): (bool, u64)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u64.** 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: u64, y: u64): (bool, u64)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return bool.** If the operation was successful.
* **@return u64.** 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: u64, y: u64, z: u64): (bool, u64)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@param z:** The divisor.
* **@return bool.** If the operation was successful.
* **@return u64.** 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. Checks for zero division and overflow.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@param z:** The divisor.
* **@return bool.** If the operation was successful.
* **@return u64.** 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: u64, y: u64): (bool, u64)
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@param z:** The divisor.
* **@return bool.** If the operation was successful.
* **@return u64.** 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: u64, y: u64): u64
```

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

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

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

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u64.** The result of `x` / `y`.

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

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

```rust
public fun div_up(a: u64, b: u64): u64
```

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return u64.** The result of `x` / `y`.

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

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

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@param z:** The divisor
* **@return u64.** 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: u64, y: u64, z: u64): u64
```

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

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

**It returns the lowest number.**

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

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

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

**It returns the largest number.**

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

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

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

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

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

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

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

**Performs |x - y|.**

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

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

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

**Performs n^e.**

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

* **@param n:** The base.
* **@param e:** The exponent.
* **@return u128.** 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<u64>): u64
```

* **@param nums:** A vector of numbers.
* **@return u64.** 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: u64, y: u64): u64
```

* **@param** x: The first operand.
* **@param y**: The second operand.
* **@return u64.** (`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<u64>): u64
```

* **@param nums**: A vector of numbers.
* **@return u64.** 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: u64): u64
```

* **@param a:** The operand.
* **@return u64.** 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(a: u64): u64
```

* **@param a:** The operand.
* **@return u64.** 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(value: u64): 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(value: u64): 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(value: u64): 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(value: u64): 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: u64): 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: u64): u8
```

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


---

# 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.interestprotocol.com/overview/deprecated/sui-tears/math/math64.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.
