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

The 32-bit unsigned fixed point number type.

Additional Information

Represented by an underlying u32 number.

Fields

underlying: u32

The underlying value representing the UFP32 type.

Implementations

fn bits() -> u64

The size of this type in bits.

Returns

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

Examples

``sway
use libraries::fixed_point::ufp32::UFP32;

fn foo() {
let bits = UFP32::bits();
assert(bits == 32);
}

fn denominator() -> u32

Convenience function to know the denominator.

Returns

  • [u32] - The value of the denominator for the UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let denominator = UFP32::denominator();
    assert(denominator == 65536u32);
}

fn max() -> Self

The largest value that can be represented by this type.

Returns

  • [UFP32] - The newly created UFP32 struct.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::max();
    assert(ufp32.underlying() == u32::max());
}

fn min() -> Self

The smallest value that can be represented by this type.

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::min();
    assert(ufp32.underlying() == u32::min());
}

fn zero() -> Self

The zero value of this type.

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::zero();
    assert(ufp32.underlying() == 0u32);
}

fn is_zero(self) -> bool

Returns whether a UFP32 is set to zero.

Returns

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

Examples

use sway_libs::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::zero();
    assert(ufp32.is_zero());
}

fn underlying(self) -> u32

Returns the underlying u32 representing the UFP32.

Returns

  • [u32] - The u32 representing the UFP32.

Examples

use sway_libs::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::zero();
    assert(ufp32.underlying() == 0u32);
}

fn from_uint(uint: u32) -> Self

Creates UFP32 that corresponds to a unsigned integer.

Arguments

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

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::from_uint(1u32);
    assert(ufp32.underlying() == 65536u32);
}

fn recip(number: UFP32) -> Self

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

Arguments

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

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::from_uint(128u32);
    let recip = UFP32::recip(ufp32);
    assert(recip.underlying() == 512u32);
}

fn trunc(self) -> Self

Returns the integer part of self.

Additional Information

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

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::from_uint(128u32);
    let trunc = ufp32.trunc();
    assert(trunc.underlying() == 0);
}

fn floor(self) -> Self

Returns the largest integer less than or equal to self.

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::from_uint(128u32);
    let floor = ufp32.floor();
    assert(floor.underlying() == 0);
}

fn fract(self) -> Self

Returns the fractional part of self.

Returns

  • [UFP32] - the newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::from_uint(128u32);
    let fract = ufp32.fract();
    assert(fract.underlying() == 0);
}

fn ceil(self) -> Self

Returns the smallest integer greater than or equal to self.

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::from_uint(128u32);
    let ceil = ufp32.ceil();
    assert(ceil.underlying() = 65536u32);
}

fn round(self) -> Self

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

Returns

  • [UFP32] - The newly created UFP32 type.

Examples

use libraries::fixed_point::ufp32::UFP32;

fn foo() {
    let ufp32 = UFP32::from_uint(128_u32);
    let round = ufp32.round();
    assert(round.underlying() == 0);
}

Trait Implementations

fn from(underlying: u32) -> Self

Creates UFP32 from u32. Note that UFP32::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 UFP32 to a UFP32. Panics on overflow.

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

Subtract a UFP32 from a UFP32. Panics of overflow.

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

Multiply a UFP32 with a UFP32. Panics of overflow.

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

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

fn sqrt(self) -> Self

Sqaure root for UFP32

fn exp(exponent: Self) -> Self

Exponent function. e ^ x

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

Power function. x ^ exponent