pub struct I128 {
    /// The underlying unsigned number representing the `I128` type.
    underlying: U128,
}
Expand description

The 128-bit signed integer type.

Additional Information

Represented as an underlying U128 value.
Actual value is underlying value minus 2 ^ 127
Max value is 2 ^ 127 - 1, min value is - 2 ^ 127

Fields

underlying: U128

The underlying unsigned number representing the I128 type.

Implementations

fn indent() -> U128

The underlying value that corresponds to zero value.

Returns

  • [U128] - The unsigned integer value representing a zero value.

Examples

use sway_libs::signed_integers::i128::I128;
use std::U128::*;

fn foo() {
    let zero = I128::indent();
    assert(zero == U128::from((1, 0)));
}

fn bits() -> u64

The size of this type in bits.

Returns

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

Examples

``sway
use sway_libs::signed_integers::i128::I128;

fn foo() {
let bits = I128::bits();
assert(bits == 128);
}

fn from_uint(underlying: U128) -> Self

Helper function to get a positive value from an unsigned number.

Arguments

  • underlying: [U128] - The unsigned number to become the underlying value for the I128.

Returns

  • [I128] - The newly created I128 struct.

Examples

use sway_libs::signed_integers::i128::I128;
use std::U128::*;

fn foo() {
    let underlying = U128::from((0, 1));
    let i128 = I128::from_uint(underlying);
    assert(i128.underlying() == underlying);
}

fn max() -> Self

The largest value that can be represented by this integer type.

Returns

  • [I128] - The newly created I128 struct.

Examples

use sway_libs::signed_integers::i128::I128;
use std::U128::*;

fn foo() {
    let i128 = I128::max();
    assert(i128.underlying() == U128::max());
}

fn min() -> Self

The smallest value that can be represented by this integer type.

Returns

  • [I128] - The newly created I128 type.

Examples

use sway_libs::signed_integers::i128::I128;
use std::U128::*;

fn foo() {
    let i128 = I128::min();
    assert(i128.underlying() == U128::min());
}

fn neg_from(value: U128) -> Self

Helper function to get a negative value of an unsigned number.

Arguments

  • value: [U128] - The unsigned number to negate.

Returns

  • [I128] - The newly created I128 struct.

Examples

use sway_libs::signed_integers::i128::I128;
use std::U128::*;

fn foo() {
    let underlying = U128::from((1, 0));
    let i128 = I128::neg_from(underlying);
    assert(i128.underlying() == U128::from((0, 0)));
}

fn new() -> Self

Initializes a new, zeroed I128.

Additional Information

The zero value of I128 is U128::from((1, 0)).

Returns

  • [I128] - The newly created I128 struct.

Examples

use sway_libs::signed_integers::i128::I128;
use std::U128::*;

fn foo() {
    let i128 = I128::new();
    assert(i128.underlying() == U128::from(1, 0));
}

fn zero() -> Self

The zero value I128.

Returns

  • [I128] - The newly created I128 type.

Examples

use sway_libs::signed_integers::i128::I128;
use std::u128::U128;

fn foo() {
    let i128 = I128::zero();
    assert(i128.underlying() == U128::from((1, 0)));
}

fn is_zero(self) -> bool

Returns whether a I128 is set to zero.

Returns

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

Examples

use sway_libs::signed_integers::i128::I128;

fn foo() {
    let i128 = I128::zero();
    assert(i128.is_zero());
}

fn underlying(self) -> U128

Returns the underlying u128 representing the I128.

Returns

  • [u128] - The u128 representing the I128.

Examples

use sway_libs::signed_integers::i128::I128;
use std::u128::U128;

fn foo() {
    let i128 = I128::zero();
    assert(i128.underlying() == U128::from((1, 0)));
}

Trait Implementations

fn from(value: U128) -> Self

Helper function to get a signed number from with an underlying

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

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

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

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

Multiply a I128 with a I128. Panics of overflow.

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

Subtract a I128 from a I128. Panics of overflow.

fn twos_complement(self) -> Self