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

The 64-bit signed fixed point number type.

Additional Information

Represented by an underlying UFP32 number and a boolean.

Fields

underlying: UFP32

The underlying value representing the IFP64 type.

non_negative: bool

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

Implementations

fn bits() -> u64

The size of this type in bits.

Returns

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

Examples

``sway
use sway_libs::fixed_point::ifp64::IFP64;

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

fn max() -> Self

The largest value that can be represented by this type.

Returns

  • [IFP64] - The newly created IFP64 struct.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::max();
    assert(ifp64.underlying() == UFP32::max());
}

fn min() -> Self

The smallest value that can be represented by this type.

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::min();
    assert(ifp64.underlying() == UFP32::min());
}

fn zero() -> Self

The zero value of this type.

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::zero();
    assert(ifp64.underlying() == UFP32::zero());
}

fn is_zero(self) -> bool

Returns whether a IFP64 is set to zero.

Returns

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

Examples

use sway_libs::fixed_point::ifp64::IFP64;

fn foo() {
    let ifp64 = IFP64::zero();
    assert(ifp64.is_zero());
}

fn sign_reverse(self) -> Self

Inverts the sign for this type.

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::ifp64::IFP64;

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

fn underlying(self) -> UFP32

Returns the underlying UFP32 representing the IFP64.

Returns

  • [UFP32] - The UFP32 representing the IFP64.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::zero();
    assert(ifp64.underlying() == UFP32::zero());
}

fn non_negative(self) -> bool

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

Returns

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

Examples

use sway_libs::fixed_point::ifp64::IFP64;

fn foo() {
    let ifp64 = IFP64::zero();
    assert(ifp64.non_negative() == false);
}

fn from_uint(uint: u32) -> Self

Creates IFP64 that corresponds to a unsigned integer.

Arguments

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

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

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

fn recip(number: IFP64) -> Self

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

Arguments

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

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::from_uint(128u32);
    let recip = IFP64::recip(ifp64);
    assert(recip.underlying() == UFP32::recip(UFP32::from(128u32)));
}

fn trunc(self) -> Self

Returns the integer part of self.

Additional Information

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

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64:IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::from_uint(128u32);
    let trunc = ifp64.trunc();
    assert(trunc.underlying() == UFP32::from(128u32).trunc());
}

fn floor(self) -> Self

Returns the largest integer less than or equal to self.

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::from_uint(128u32);
    let floor = ifp64.floor();
    assert(floor.underlying() == UFP32::from(128u32).trunc().underlying());
}

fn fract(self) -> Self

Returns the fractional part of self.

Returns

  • [IFP64] - the newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::from_uint(128u32);
    let fract = ifp64.fract();
    assert(fract.underlying() == UFP32::from(128u32).fract());
}

fn ceil(self) -> Self

Returns the smallest integer greater than or equal to self.

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::from_uint(128u32);
    let ceil = ifp64.ceil();
    assert(ceil.underlying() = UFP32::from(128u32).ceil());
}

fn round(self) -> Self

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

Returns

  • [IFP64] - The newly created IFP64 type.

Examples

use sway_libs::fixed_point::{ifp64::IFP64, ufp32::UFP32};

fn foo() {
    let ifp64 = IFP64::from_uint(128_u32);
    let round = ifp64.round();
    assert(round.underlying() == UFP32::from(128u32).round());
}

Trait Implementations

fn from(value: UFP32) -> Self

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

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

Subtract a IFP64 from a IFP64. Panics of overflow.

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

Multiply a IFP64 with a IFP64. Panics of overflow.

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

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

fn exp(exponent: Self) -> Self

Exponent function. e ^ x

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

Power function. x ^ exponent