pub struct UFP128 {
    /// The underlying value representing the `UFP128` type.
    underlying: U128,
}
Expand description

The 128-bit unsigned fixed point number type.

Additional Information

Represented by an underlying U128 number.

Fields

underlying: U128

The underlying value representing the UFP128 type.

Implementations

fn zero() -> Self

The zero value of this type.

Returns

  • [UFP128] - The newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp128 = UFP128::zero();
    assert(ufp128.underlying() == U128::from((0, 0)));
}

fn min() -> Self

The smallest value that can be represented by this type.

Returns

  • [UFP128] - The newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;
use std::u128::U128;

fn foo() {
    let ufp128 = UFP128::min();
    assert(ufp128.underlying() == U128::min());
}

fn max() -> Self

The largest value that can be represented by this type.

Returns

  • [UFP128] - The newly created UFP128 struct.

Examples

use libraries::fixed_point::ufp128::UFP128;
use std::u128::U128;

fn foo() {
    let ufp128 = UFP128::max();
    assert(ufp128.underlying() == U128::max());
}

fn bits() -> u64

The size of this type in bits.

Returns

[u64] - The defined size of the UFP128 type.

Examples

``sway
use libraries::fixed_point::ufp128::UFP128;

fn foo() {
let bits = UFP128::bits();
assert(bits == 128);
}

fn is_zero(self) -> bool

Returns whether a UFP128 is set to zero.

Returns

  • [bool] -> True if the UFP128 is zero, otherwise false.

Examples

use sway_libs::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp128 = UFP128::zero();
    assert(ufp128.is_zero());
}

fn underlying(self) -> U128

Returns the underlying U128 representing the UFP128.

Returns

  • [U128] - The U128 representing the UFP128.

Examples

use sway_libs::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp128 = UFP128::zero();
    assert(ufp128.underlying() == U128::zero());
}

fn from_uint(uint: u64) -> Self

Creates UFP128 that corresponds to a unsigned integer.

Arguments

  • uint: [u64] - The unsigned number to become the underlying value for the UFP128.

Returns

  • [UFP128] - The newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;
use std::u128::U128;

fn foo() {
    let ufp128 = UFP128::from_uint(1);
    assert(ufp128.underlying() == U128::from((1, 0));
}

fn recip(number: UFP128) -> Self

Takes the reciprocal (inverse) of a number, 1/x.

Arguments

  • number: [UFP128] - The value to create the reciprocal from.

Returns

  • [UFP128] - The newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp64 = UFP128::from_uint(128);
    let recip = UFP64::recip(ufp64);
    assert(recip.underlying() == U128::from((33554432, 0));
}

fn floor(self) -> Self

Returns the largest integer less than or equal to self.

Returns

  • [UFP128] - The newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp128 = UFP128::from_uint(128);
    let floor = ufp128.floor();
    assert(floor.underlying() == U128::from((0,0)));
}

fn ceil(self) -> Self

Returns the smallest integer greater than or equal to self.

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp128 = UFP128::from_uint(128);
    let ceil = ufp128.ceil();
    assert(ceil.underlying() = U128::from((4294967296, 0)));
}

fn round(self) -> Self

Returns the nearest integer to self. Round half-way cases away from zero.

Returns

  • [UFP128] - The newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp128 = UFP128::from_uint(128);
    let round = ufp128.round();
    assert(round.underlying() == U128::from(0,0)));
}

fn trunc(self) -> Self

Returns the integer part of self.

Additional Information

This means that non-integer numbers are always truncated towards zero.

Returns

  • [UFP128] - The newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp128 = UFP128::from_uint(128);
    let trunc = ufp128.trunc();
    assert(trunc.underlying() == U128::from((0,0)));
}

fn fract(self) -> Self

Returns the fractional part of self.

Returns

  • [UFP128] - the newly created UFP128 type.

Examples

use libraries::fixed_point::ufp128::UFP128;

fn foo() {
    let ufp128 = UFP128::from_uint(128);
    let fract = ufp128.fract();
    assert(fract.underlying() == U128::from((0, 0)));
}

Trait Implementations

fn from(int_fract_tuple: (u64, u64)) -> Self

fn from(u128: U128) -> Self

fn eq(self, other: Self) -> bool

fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}

fn gt(self, other: Self) -> bool

fn lt(self, other: Self) -> bool

fn add(self, other: Self) -> Self

Add a UFP128 to a UFP128. Panics on overflow.

fn subtract(self, other: Self) -> Self

Subtract a UFP128 from a UFP128. Panics of overflow.

fn multiply(self, other: Self) -> Self

Multiply a UFP128 with a UFP128. Panics on overflow.

fn divide(self, divisor: Self) -> Self

Divide a UFP128 by a UFP128. Panics if divisor is zero.

fn sqrt(self) -> Self

fn pow(self, exponent: u32) -> Self

fn exp(exponent: Self) -> Self