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

The 64-bit unsigned fixed point number type.

Additional Information

Represented by an underlying u64 number.

Fields

underlying: u64

The underlying value representing the UFP64 type.

Implementations

fn bits() -> u64

The size of this type in bits.

Returns

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

Examples

``sway
use libraries::fixed_point::ufp64::UFP64;

fn foo() {
let bits = UFP64::bits();
assert(bits == 64);
}

fn denominator() -> u64

Convenience function to know the denominator.

Returns

  • [u64] - The value of the denominator for the UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let denominator = UFP64::denominator();
    assert(denominator == 4294967296);
}

fn max() -> Self

The largest value that can be represented by this type.

Returns

  • [UFP64] - The newly created UFP64 struct.

Examples

use v::UFP64;

fn foo() {
    let ufp64 = UFP64::max();
    assert(ufp64.underlying() == u64::max());
}

fn min() -> Self

The smallest value that can be represented by this type.

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::min();
    assert(ufp64.underlying() == u64::min());
}

fn zero() -> Self

The zero value of this type.

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::zero();
    assert(ufp64.underlying() == 0);
}

fn is_zero(self) -> bool

Returns whether a UFP64 is set to zero.

Returns

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

Examples

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

fn foo() {
    let ufp64 = UFP64::zero();
    assert(ufp64.is_zero());
}

fn underlying(self) -> u64

Returns the underlying u64 representing the UFP64.

Returns

  • [u64] - The u64 representing the UFP64.

Examples

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

fn foo() {
    let ufp64 = UFP64::zero();
    assert(ufp64.underlying() == 0);
}

fn from_uint(uint: u64) -> Self

Creates UFP64 that corresponds to a unsigned integer.

Arguments

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

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::from_uint(1);
    assert(ufp64.underlying() == 4294967296);
}

fn recip(number: UFP64) -> Self

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

Arguments

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

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::from_uint(128);
    let recip = UFP64::recip(ufp64);
    assert(recip.underlying() == 33554432);
}

fn trunc(self) -> Self

Returns the integer part of self.

Additional Information

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

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::from_uint(128);
    let trunc = ufp64.trunc();
    assert(trunc.underlying() == 0);
}

fn floor(self) -> Self

Returns the largest integer less than or equal to self.

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::from_uint(128);
    let floor = ufp64.floor();
    assert(floor.underlying() == 0);
}

fn fract(self) -> Self

Returns the fractional part of self.

Returns

  • [UFP64] - the newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::from_uint(128);
    let fract = ufp64.fract();
    assert(fract.underlying() == 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::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::from_uint(128);
    let ceil = ufp64.ceil();
    assert(ceil.underlying() = 4294967296);
}

fn round(self) -> Self

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

Returns

  • [UFP64] - The newly created UFP64 type.

Examples

use libraries::fixed_point::ufp64::UFP64;

fn foo() {
    let ufp64 = UFP64::from_uint(128);
    let round = ufp64.round();
    assert(round.underlying() == 0);
}

Trait Implementations

fn from(underlying: u64) -> Self

Creates UFP64 from u64. Note that UFP64::from(1) is 1 / 2^32 and not 1.

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 UFP64 to a UFP64. Panics on overflow.

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

Subtract a UFP64 from a UFP64. Panics of overflow.

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

Multiply a UFP64 with a UFP64. Panics of overflow.

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

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

fn sqrt(self) -> Self

Sqaure root for UFP64

fn exp(exponent: Self) -> Self

Exponent function. e ^ x

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

Power function. x ^ exponent