Struct sway_libs::signed_integers::i256::I256
pub struct I256 {
underlying: u256,
}
Expand description
The 256-bit signed integer type.
Additional Information
Represented as an underlying u256 value.
Actual value is underlying value minus 2 ^ 255
Max value is 2 ^ 255 - 1, min value is - 2 ^ 255
Fields
underlying: u256
Implementations
fn indent() -> u256
fn indent() -> u256
The underlying value that corresponds to zero value.
Additional Information
The zero value for I256 is 0x8000000000000000000000000000000000000000000000000000000000000000u256.
Returns
- [u256] - The unsigned integer value representing a zero value.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let zero = I256::indent();
assert(zero == 0x8000000000000000000000000000000000000000000000000000000000000000u256);
}
fn bits() -> u64
fn bits() -> u64
The size of this type in bits.
Returns
[u64] - The defined size of the I256
type.
Examples
``sway
use sway_libs::signed_integers::i256::I256;
fn foo() {
let bits = I256::bits();
assert(bits == 256);
}
fn from_uint(underlying: u256) -> Self
fn from_uint(underlying: u256) -> Self
Helper function to get a signed number from with an underlying.
Arguments
underlying
: [u256] - The unsigned number to become the underlying value for theI256
.
Returns
- [I256] - The newly created
I256
struct.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let underlying = 0x0000000000000000000000000000000000000000000000000000000000000001u256;
let i256 = I256::from_uint(underlying);
assert(i256.underlying() == underlying);
}
fn max() -> Self
fn max() -> Self
The largest value that can be represented by this integer type.
Returns
- [I256] - The newly created
I256
struct.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let i256 = I256::max();
assert(i256.underlying() == u256::max());
}
fn min() -> Self
fn min() -> Self
The smallest value that can be represented by this integer type.
Returns
- [I256] - The newly created
I256
type.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let i256 = I256::min();
assert(i256.underlying() == u256::min());
}
fn neg_try_from(value: u256) -> Option<Self>
fn neg_try_from(value: u256) -> Option<Self>
Helper function to get a negative value of an unsigned number.
Arguments
value
: [u256] - The unsigned number to negate.
Returns
- [Option] - The newly created
I256
struct.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let underlying = 0x0000000000000000000000000000000000000000000000000000000000000000u256;
let i256 = I256::neg_try_from(underlying).unwrap();
assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256);
}
fn new() -> Self
fn new() -> Self
Initializes a new, zeroed I256.
Additional Information
The zero value of I256 is 0x8000000000000000000000000000000000000000000000000000000000000000u256.
Returns
- [I256] - The newly created
I256
struct.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let i256 = I256::new();
assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256);
}
fn zero() -> Self
fn zero() -> Self
The zero value I256
.
Returns
- [I256] - The newly created
I256
type.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let i256 = I256::zero();
assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256);
}
fn is_zero(self) -> bool
fn is_zero(self) -> bool
Returns whether a I256
is set to zero.
Returns
- [bool] -> True if the
I256
is zero, otherwise false.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let i256 = I256::zero();
assert(i256.is_zero());
}
fn underlying(self) -> u256
fn underlying(self) -> u256
Returns the underlying u256
representing the I256
.
Returns
- [u256] - The
u256
representing theI256
.
Examples
use sway_libs::signed_integers::i256::I256;
fn foo() {
let i256 = I256::zero();
assert(i256.underlying() == 0x8000000000000000000000000000000000000000000000000000000000000000u256);
}
Trait Implementations
impl Eq for I256
impl Eq for I256
fn eq(self, other: Self) -> bool
fn neq(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, otherwisefalse
.
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);
}
impl OrdEq for I256
impl OrdEq for I256
fn ge(self, other: Self) -> bool
fn ge(self, other: Self) -> bool
Evaluates if one value of the same type is greater or equal to than another.
Additional Information
This trait requires that the Ord
and Eq
traits are implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
ifself
is greater than or equal toother
, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
impl Ord for MyStruct {
fn gt(self, other: Self) -> bool {
self.val > other.val
}
}
impl OrdEq for MyStruct {}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 10 };
let result = struct1 >= struct2;
assert(result);
}
fn le(self, other: Self) -> bool
fn le(self, other: Self) -> bool
Evaluates if one value of the same type is less or equal to than another.
Additional Information
This trait requires that the Ord
and Eq
traits are implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
ifself
is less than or equal toother
, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
impl Ord for MyStruct {
fn lt(self, other: Self) -> bool {
self.val < other.val
}
}
impl OrdEq for MyStruct {}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 10 };
let result = struct1 <= struct2;
assert(result);
}
impl Divide for I256
impl Divide for I256
fn divide(self, divisor: Self) -> Self
fn divide(self, divisor: Self) -> Self
Divide a I256 by a I256. Panics if divisor is zero.
impl Multiply for I256
impl Multiply for I256
fn multiply(self, other: Self) -> Self
fn multiply(self, other: Self) -> Self
Multiply a I256 with a I256. Panics of overflow.
impl Subtract for I256
impl Subtract for I256
fn subtract(self, other: Self) -> Self
fn subtract(self, other: Self) -> Self
Subtract a I256 from a I256. Panics of overflow.