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

The 256-bit signed fixed point number type.

Additional Information

Represented by an underlying UFP128 number and a boolean.

Fields

underlying: UFP128

The underlying value representing the IFP256 type.

non_negative: bool

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

Implementations

fn bits() -> u64

The size of this type in bits.

Returns

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

Examples

``sway
use sway_libs::fixed_point::ifp256::IFP256;

fn foo() {
let bits = IFP256::bits();
assert(bits == 136);
}

fn max() -> Self

The largest value that can be represented by this type.

Returns

  • [IFP256] - The newly created IFP256 struct.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::max();
    assert(ifp256.underlying() == UFP128::max());
}

fn min() -> Self

The smallest value that can be represented by this type.

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::min();
    assert(ifp256.underlying() == UFP128::min());
}

fn zero() -> Self

The zero value of this type.

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::zero();
    assert(ifp256.underlying() == UFP128::zero());
}

fn sign_reverse(self) -> Self

Inverts the sign for this type.

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::ifp256::IFP256;

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

fn is_zero(self) -> bool

Returns whether a IFP256 is set to zero.

Returns

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

Examples

use sway_libs::fixed_point::ifp256::IFP256;

fn foo() {
    let ifp256 = IFP256::zero();
    assert(ifp256.is_zero());
}

fn underlying(self) -> UFP128

Returns the underlying UFP128 representing the IFP256.

Returns

  • [UFP128] - The UFP128 representing the IFP256.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128};

fn foo() {
    let ifp256 = IFP256::zero();
    assert(ifp256.underlying() == UFP128::zero());
}

fn non_negative(self) -> bool

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

Returns

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

Examples

use sway_libs::fixed_point::ifp256::IFP256;

fn foo() {
    let ifp256 = IFP256::zero();
    assert(ifp256.non_negative() == false);
}

fn from_uint(uint: u64) -> Self

Creates IFP256 that corresponds to a unsigned integer.

Arguments

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

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::from_uint(1);
    assert(ifp256.underlying() == UFP128::from_uint(1));
}

fn recip(number: IFP256) -> Self

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

Arguments

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

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::from_uint(128);
    let recip = IFP256::recip(ifp256);
    assert(recip.underlying() == UFP128::recip(UFP128::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

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::from_uint(128);
    let trunc = ifp256.trunc();
    assert(trunc.underlying() == UFP128::from(128).trunc());
}

fn floor(self) -> Self

Returns the largest integer less than or equal to self.

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::from_uint(128);
    let floor = ifp256.floor();
    assert(floor.underlying() == UFP128::from(128).floor());
}

fn fract(self) -> Self

Returns the fractional part of self.

Returns

  • [IFP256] - the newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::from_uint(128);
    let fract = ifp256.fract();
    assert(fract.underlying() == UFP128::from(128).fract());
}

fn ceil(self) -> Self

Returns the smallest integer greater than or equal to self.

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::from_uint(128);
    let ceil = ifp256.ceil();
    assert(ceil.underlying() = UFP128::from(128).ceil().underlying());
}

fn round(self) -> Self

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

Returns

  • [IFP256] - The newly created IFP256 type.

Examples

use sway_libs::fixed_point::{ifp256::IFP256, ufp128::UFP128}

fn foo() {
    let ifp256 = IFP256::from_uint(128);
    let round = ifp256.round();
    assert(round.underlying() == UFP128::from(128).round().underlying());
}

Trait Implementations

fn from(value: UFP128) -> Self

Creates IFP256 from UFP128. Note that IFP256::from(1) is 1 / 2^128 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 IFP256 to a IFP256. Panics on overflow.

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

Subtract a IFP256 from a IFP256. Panics of overflow.

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

Multiply a IFP256 with a IFP256. Panics of overflow.

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

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

fn exp(exponent: Self) -> Self

Exponent function. e ^ x

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

Power function. x ^ exponent