# Fixed Point 64

A library to perform math operations over an unsigned integer with 64-bit precision. Any operation that results in a number larger than the maximum unsigned 128 bit, will be considered an overflow and throw.

## Structs

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

```rust
struct FixedPoint64 has copy, drop, store { value: u128 }
```

* **value** - The number.

## Interface

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

**It returns the raw u128 value.**

```rust
public fun value(self: FixedPoint64): u128
```

* **@param self:** A FixedPoint64
* **@return u128.** The raw u128 value.

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

**Creates a FixedPoint64 from a u128 number. It scales the number.**

```rust
public fun from(value: u128): FixedPoint64
```

* **@param value:** A u128 number.
* **@return A FixedPoint64.** calculated by right shifting - `value` << 64.

**Aborts**

* The left-shifted `value` is larger than `MAX_U128`.

### <mark style="color:blue;">from\_raw\_value</mark>

**Creates a FixedPoint64 from a u128 `value`. It does not scale the `value`.**

```rust
public fun from_raw_value(value: u128): FixedPoint64
```

* **@param value:** A u128 number.
* **@return FixedPoint64.** It wraps the u128.

### <mark style="color:blue;">from\_rational</mark>

**Creates a FixedPoint64 from a rational number specified by a `numerator` and`denominator`.**

{% hint style="warning" %}
0.0125 will round down to 0.012 instead of up to 0.013.
{% endhint %}

```rust
public fun from_rational(numerator: u128, denominator: u128): FixedPoint64
```

* **@param numerator:** The numerator of the rational number.
* **@param denominator:** The denominator of the rational number.
* **@return FixedPoint64.** A FixedPoint64 from (`numerator` << 64) / `denominator.`

**Aborts**

* if the denominator is zero
* if the numerator / denominator is zero
* if the numerator is nonzero and the ratio is not in the range 2^-64 .. 2^64-1

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

**Converts a FixedPoint64 into a u128 number to the closest integer.**

```rust
public fun to_u128(self: FixedPoint64): u128
```

* **@param self.** A FixedPoint64.
* **@return u128.**

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

**Converts a FixedPoint64 into a u128 number rounding down.**

```rust
public fun to_u128_down(self: FixedPoint64): u128
```

* **@param self.** A FixedPoint64.
* **@return u128.**

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

**Converts a FixedPoint64 into a u128 number rounding up.**

```rust
public fun to_u128_up(self: FixedPoint64): u128
```

* **@param self.** A FixedPoint64.
* **@return u128.**

### <mark style="color:blue;">is\_zero</mark>

**Checks if `self` is zero.**

```rust
public fun is_zero(self: FixedPoint64): bool
```

* **@param self.** A FixedPoint64.
* **@return bool.** If the `self.value` is zero.

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

**Checks if `x` is equal to `y`.**

```rust
public fun eq(x: FixedPoint64, y: FixedPoint64): bool
```

* **@param x:** A FixedPoint64.
* **@param y:** A FixedPoint64.
* **@return bool**. If the values are equal.

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

**Checks if `x` is smaller or equal to `y`.**

```rust
public fun lt(x: FixedPoint64, y: FixedPoint64): bool
```

* **@param x:** A FixedPoint64.
* **@param y:** A FixedPoint64.
* **@return bool.** If `x` is smaller than `y`.

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

**Checks if `x` is bigger than `y`.**

```rust
public fun gt(x: FixedPoint64, y: FixedPoint64): bool
```

* **@param x:** A FixedPoint64.
* **@param y:** A FixedPoint64.
* **@return bool.** If `x` is bigger or equal to `y`.

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

**Checks if `x` is smaller or equal to `y`.**

```rust
public fun lte(x: FixedPoint64, y: FixedPoint64): bool
```

* **@param x:** A FixedPoint64.
* **@param y:** A FixedPoint64.
* **@return bool.** If `x` is smaller or equal to `y`.

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

**Checks if `x` is bigger or equal to `y`.**

```rust
public fun gte(x: FixedPoint64, y: FixedPoint64): bool
```

* **@param x:** A FixedPoint64.
* **@param y:** A FixedPoint64.
* **@return bool.** If `x` is bigger or equal to `y`.

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

**It returns the larger of the two arguments.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return FixedPoint64**. The larger argument.

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

**It returns the smaller of the two arguments.**

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

* **@param x:** The first operand.
* **@param y:** The second operand.
* **@return FixedPoint64**. The smaller argument.

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

**It returns `x` - `y`.**

```rust
public fun sub(x: FixedPoint64, y: FixedPoint64): FixedPoint64
```

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

**Aborts**

* `y` > `x`

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

**It returns `x` + `y`.**

```rust
public fun add(x: FixedPoint64, y: FixedPoint64): FixedPoint64
```

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

**Aborts**

* `y` + `x` >= `MAX_U128`

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

**It returns `x` \* `y`.**

{% hint style="warning" %}
Use {mul\_128} if you think the values can overflow.
{% endhint %}

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

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

**Aborts**

* inner values overflow.

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

**It returns `x` / `y`.**

```rust
public fun div(x: FixedPoint64, y: FixedPoint64): FixedPoint64
```

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

**Aborts**

* if `y` is zero.

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

**Specialized function for `x` \* `y` / `z` that omits intermediate shifting.**

```rust
public fun mul_div(x: FixedPoint64, y: FixedPoint64, z: FixedPoint64): FixedPoint64
```

* **@param x:** A FixedPoint64.
* **@param y:** A FixedPoint64.
* **@param z:** The third operand.
* **@return FixedPoint64**. The result of `x` \* `y` / `z`.

**Aborts**

* if z is zero.

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

It returns `x` \* `y.`It multiplies a u128 number with a FixedPoint64. **It truncates the fractional part of the product. E.g. - 9 \* 0.333 = 2.**

```rust
public fun mul_u128(x: u128, y: FixedPoint64): u128
```

* **@param x:** A FixedPoint64.
* **@param y:** A FixedPoint64.
* **@return u128.** The result of `x` \* `y` without the 64-bit precision.

**Aborts**

* if the result is larger or equal to `MAX_U128`.

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

**It returns `numerator` / `denominator` rounded down. It divides a FixedPoint64 by a u128 number.**

```rust
public fun div_down_u128(numerator: u128, denominator: FixedPoint64): u128
```

* **@param numerator:** The first operand, a u128 number.
* **@param denominator:** The first operand, a u128 number.
* **@return u128.** The result of `numerator` / `denominator` without the 64-bit precision.

**Aborts**

* if the result is larger or equal to `MAX_U128`.
* if the `denominator` is zero.

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

**It returns `numerator` / `denominator` rounded up. It divides a FixedPoint64 by a u128 number.**

```rust
public fun div_up_u128(numerator: u128, denominator: FixedPoint64): u128
```

* **@param numerator:** The first operand, a u128 number.
* **@param denominator:** The first operand, a u128 number.
* **@return u128.** The result of `numerator` / `denominator` without the 64-bit precision.

**Aborts**

* if the result is larger or equal to `MAX_U128`.
* if the `denominator` is zero.

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

**It returns `base` \*\* `exponent`.**

```rust
public fun pow(base: FixedPoint64, exponent: u64): FixedPoint64
```

* **@param base**: The base.
* **@param exponent:** The exponent.
* **@return FixedPoint64.** The result of `base` \*\* `exponent`.

**Aborts**

* if the end result is higher than `MAX_U128`.

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

**Square root of `x`.**

```rust
public fun sqrt(x: FixedPoint64): FixedPoint64
```

* **@param x:** The operand.
* **@return FixedPoint64.** The result of the square root.

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

**It performs e^x. Exponent function with a precision of 9 digits.**

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

* **@param x:** The operand.
* **@return FixedPoint64.** The result of e^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/fixed-point-64.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.
