pub struct IFP128 {
    /// The underlying value representing the `IFP128` type.
    underlying: UFP64,
    /// The underlying boolean representing a negative value for the `IFP128` type.
    non_negative: bool,
}
Expand description

The 128-bit signed fixed point number type.

Additional Information

Represented by an underlying UFP64 number and a boolean.

Fields

underlying: UFP64

The underlying value representing the IFP128 type.

non_negative: bool

The underlying boolean representing a negative value for the IFP128 type.

Implementations

fn bits() -> u64

The size of this type in bits.

Returns

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

Examples

``sway
use sway_libs::fixed_point::ifp128::IFP128;

fn foo() {
let bits = IFP128::bits();
assert(bits == 72);
}

fn max() -> Self

The largest value that can be represented by this type.

Returns

  • [IFP128] - The newly created IFP128 struct.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::max();
    assert(ifp128.underlying() == UFP64::max());
}

fn min() -> Self

The smallest value that can be represented by this type.

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::min();
    assert(ifp128.underlying() == UFP64::min());
}

fn zero() -> Self

The zero value of this type.

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::zero();
    assert(ifp128.underlying() == UFP64::zero());
}

fn sign_reverse(self) -> Self

Inverts the sign for this type.

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::ifp128::IFP128;

fn foo() {
    let ifp128 = IFP128::zero();
    assert(ifp128.non_negative());
    let reverse = ifp128.sign_inverse();
    assert(!reverse.non_negative());
}

fn is_zero(self) -> bool

Returns whether a IFP128 is set to zero.

Returns

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

Examples

use sway_libs::fixed_point::ifp128::IFP128;

fn foo() {
    let ifp128 = IFP128::zero();
    assert(ifp128.is_zero());
}

fn underlying(self) -> UFP64

Returns the underlying UFP64 representing the IFP128.

Returns

  • [UFP64] - The UFP64 representing the IFP128.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::zero();
    assert(ifp128.underlying() == UFP64::zero());
}

fn non_negative(self) -> bool

Returns the underlying bool representing the postive or negative state of the IFP128.

Returns

  • [bool] - The bool representing whether the IFP128 is non-negative or not.

Examples

use sway_libs::fixed_point::ifp128::IFP128;

fn foo() {
    let ifp128 = IFP128::zero();
    assert(ifp128.non_negative() == false);
}

fn from_uint(uint: u64) -> Self

Creates IFP128 that corresponds to a unsigned integer.

Arguments

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

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

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

fn recip(number: IFP128) -> Self

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

Arguments

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

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::from_uint(128);
    let recip = IFP128::recip(ifp128);
    assert(recip.underlying() == UFP64::recip(UFP64::from(128)));
}

fn trunc(self) -> Self

Returns the integer part of self.

Additional Information

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

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::from_uint(128);
    let trunc = ifp128.trunc();
    assert(trunc.underlying() == UFP64::from(128).trunc());
}

fn floor(self) -> Self

Returns the largest integer less than or equal to self.

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::from_uint(128);
    let floor = ifp128.floor();
    assert(floor.underlying == UFP64::from(128).floor());
}

fn fract(self) -> Self

Returns the fractional part of self.

Returns

  • [IFP128] - the newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::from_uint(128);
    let fract = ifp128.fract();
    assert(fract.underlying() == UFP64::from(128).fract());
}

fn ceil(self) -> Self

Returns the smallest integer greater than or equal to self.

Returns

  • [IFP128] - The newly created IF128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::from_uint(128);
    let ceil = ifp128.ceil();
    assert(ceil.underlying = UFP64::from(128).ceil());
}

fn round(self) -> Self

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

Returns

  • [IFP128] - The newly created IFP128 type.

Examples

use sway_libs::fixed_point::{ifp128::IFP128, ufp64::UFP64};

fn foo() {
    let ifp128 = IFP128::from_uint(128);
    let round = ifp128.round();
    assert(round.underlying() == UFP64::from(128).round());
}

Trait Implementations

fn from(value: UFP64) -> Self

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

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

Subtract a IFP128 from a IFP128. Panics of overflow.

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

Multiply a IFP128 with a IFP128. Panics of overflow.

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

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

fn exp(exponent: Self) -> Self

Exponent function. e ^ x

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

Power function. x ^ exponent