Struct sway_libs::fixed_point::ufp64::UFP64
pub struct UFP64 {
/// The underlying value representing the `UFP64` type.
underlying: u64,
}
Expand description
The 64-bit unsigned fixed point number type.
Additional Information
Represented by an underlying u64
number.
Fields
underlying: u64
The underlying value representing the UFP64
type.
Implementations
fn bits() -> u64
fn bits() -> u64
The size of this type in bits.
Returns
[u64] - The defined size of the UFP64
type.
Examples
``sway
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let bits = UFP64::bits();
assert(bits == 64);
}
fn denominator() -> u64
fn denominator() -> u64
Convenience function to know the denominator.
Returns
- [u64] - The value of the denominator for the
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let denominator = UFP64::denominator();
assert(denominator == 4294967296);
}
fn max() -> Self
fn max() -> Self
The largest value that can be represented by this type.
Returns
- [UFP64] - The newly created
UFP64
struct.
Examples
use v::UFP64;
fn foo() {
let ufp64 = UFP64::max();
assert(ufp64.underlying() == u64::max());
}
fn min() -> Self
fn min() -> Self
The smallest value that can be represented by this type.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::min();
assert(ufp64.underlying() == u64::min());
}
fn zero() -> Self
fn zero() -> Self
The zero value of this type.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::zero();
assert(ufp64.underlying() == 0);
}
fn is_zero(self) -> bool
fn is_zero(self) -> bool
Returns whether a UFP64
is set to zero.
Returns
- [bool] -> True if the
UFP64
is zero, otherwise false.
Examples
use sway_libs::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::zero();
assert(ufp64.is_zero());
}
fn underlying(self) -> u64
fn underlying(self) -> u64
Returns the underlying u64
representing the UFP64
.
Returns
- [u64] - The
u64
representing theUFP64
.
Examples
use sway_libs::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::zero();
assert(ufp64.underlying() == 0);
}
fn from_uint(uint: u64) -> Self
fn from_uint(uint: u64) -> Self
Creates UFP64 that corresponds to a unsigned integer.
Arguments
uint
: [u64] - The unsigned number to become the underlying value for theUFP64
.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::from_uint(1);
assert(ufp64.underlying() == 4294967296);
}
fn recip(number: UFP64) -> Self
fn recip(number: UFP64) -> Self
Takes the reciprocal (inverse) of a number, 1/x
.
Arguments
number
: [UFP64] - The value to create the reciprocal from.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::from_uint(128);
let recip = UFP64::recip(ufp64);
assert(recip.underlying() == 33554432);
}
fn trunc(self) -> Self
fn trunc(self) -> Self
Returns the integer part of self
.
Additional Information
This means that non-integer numbers are always truncated towards zero.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::from_uint(128);
let trunc = ufp64.trunc();
assert(trunc.underlying() == 0);
}
fn floor(self) -> Self
fn floor(self) -> Self
Returns the largest integer less than or equal to self
.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::from_uint(128);
let floor = ufp64.floor();
assert(floor.underlying() == 0);
}
fn fract(self) -> Self
fn fract(self) -> Self
Returns the fractional part of self
.
Returns
- [UFP64] - the newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::from_uint(128);
let fract = ufp64.fract();
assert(fract.underlying() == 0);
}
fn ceil(self) -> Self
fn ceil(self) -> Self
Returns the smallest integer greater than or equal to self
.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::from_uint(128);
let ceil = ufp64.ceil();
assert(ceil.underlying() = 4294967296);
}
fn round(self) -> Self
fn round(self) -> Self
Returns the nearest integer to self
. Round half-way cases away from zero.
Returns
- [UFP64] - The newly created
UFP64
type.
Examples
use libraries::fixed_point::ufp64::UFP64;
fn foo() {
let ufp64 = UFP64::from_uint(128);
let round = ufp64.round();
assert(round.underlying() == 0);
}
Trait Implementations
impl From for UFP64
impl From for UFP64
fn from(underlying: u64) -> Self
fn from(underlying: u64) -> Self
Creates UFP64 from u64. Note that UFP64::from(1) is 1 / 2^32 and not 1.
impl Eq for UFP64
impl Eq for UFP64
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 Subtract for UFP64
impl Subtract for UFP64
fn subtract(self, other: Self) -> Self
fn subtract(self, other: Self) -> Self
Subtract a UFP64 from a UFP64. Panics of overflow.
impl Multiply for UFP64
impl Multiply for UFP64
fn multiply(self, other: Self) -> Self
fn multiply(self, other: Self) -> Self
Multiply a UFP64 with a UFP64. Panics of overflow.