crates/sui-framework/docs/std/uq32_32.md
Defines an unsigned, fixed-point numeric type with a 32-bit integer part and a 32-bit fractional part. The notation <code><a href="../std/uq32_32.md#std_uq32_32">uq32_32</a></code> and <code><a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a></code> is based on Q notation. <code>q</code> indicates it a fixed-point number. The <code>u</code> prefix indicates it is unsigned. The <code>32_32</code> suffix indicates the number of bits, where the first number indicates the number of bits in the integer part, and the second the number of bits in the fractional part--in this case 32 bits for each.
UQ32_32from_quotientfrom_intaddsubmuldivto_intint_mulint_divleltgegtto_rawfrom_raw<a name="std_uq32_32_UQ32_32"></a>
UQ32_32A fixed-point numeric type with 32 integer bits and 32 fractional bits, represented by an underlying 64 bit value. This is a binary representation, so decimal values may not be exactly representable, but it provides more than 9 decimal digits of precision both before and after the decimal point (18 digits total).
<pre><code><b>public</b> <b>struct</b> <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> <b>has</b> <b>copy</b>, drop, store </code></pre> <details> <summary>Fields</summary> <dl> <dt> <code>0: <a href="../std/u64.md#std_u64">u64</a></code> </dt> <dd> </dd> </dl> </details><a name="@Constants_0"></a>
<a name="std_uq32_32_EDenominator"></a>
<pre><code>#[error] <b>const</b> <a href="../std/uq32_32.md#std_uq32_32_EDenominator">EDenominator</a>: <a href="../std/vector.md#std_vector">vector</a><<a href="../std/u8.md#std_u8">u8</a>> = b"Quotient specified with a zero denominator"; </code></pre><a name="std_uq32_32_EQuotientTooSmall"></a>
<pre><code>#[error] <b>const</b> <a href="../std/uq32_32.md#std_uq32_32_EQuotientTooSmall">EQuotientTooSmall</a>: <a href="../std/vector.md#std_vector">vector</a><<a href="../std/u8.md#std_u8">u8</a>> = b"Quotient specified is too small, and is outside of the supported range"; </code></pre><a name="std_uq32_32_EQuotientTooLarge"></a>
<pre><code>#[error] <b>const</b> <a href="../std/uq32_32.md#std_uq32_32_EQuotientTooLarge">EQuotientTooLarge</a>: <a href="../std/vector.md#std_vector">vector</a><<a href="../std/u8.md#std_u8">u8</a>> = b"Quotient specified is too large, and is outside of the supported range"; </code></pre><a name="std_uq32_32_EOverflow"></a>
<pre><code>#[error] <b>const</b> <a href="../std/uq32_32.md#std_uq32_32_EOverflow">EOverflow</a>: <a href="../std/vector.md#std_vector">vector</a><<a href="../std/u8.md#std_u8">u8</a>> = b"Overflow from an arithmetic operation"; </code></pre><a name="std_uq32_32_EDivisionByZero"></a>
<pre><code>#[error] <b>const</b> <a href="../std/uq32_32.md#std_uq32_32_EDivisionByZero">EDivisionByZero</a>: <a href="../std/vector.md#std_vector">vector</a><<a href="../std/u8.md#std_u8">u8</a>> = b"Division by zero"; </code></pre><a name="std_uq32_32_TOTAL_BITS"></a>
The total number of bits in the fixed-point number. Used in <code><b>macro</b></code> invocations.
<pre><code><b>const</b> <a href="../std/uq32_32.md#std_uq32_32_TOTAL_BITS">TOTAL_BITS</a>: <a href="../std/u8.md#std_u8">u8</a> = 64; </code></pre><a name="std_uq32_32_FRACTIONAL_BITS"></a>
The number of fractional bits in the fixed-point number. Used in <code><b>macro</b></code> invocations.
<pre><code><b>const</b> <a href="../std/uq32_32.md#std_uq32_32_FRACTIONAL_BITS">FRACTIONAL_BITS</a>: <a href="../std/u8.md#std_u8">u8</a> = 32; </code></pre><a name="std_uq32_32_from_quotient"></a>
from_quotientCreate a fixed-point value from a quotient specified by its numerator and denominator. <code><a href="../std/uq32_32.md#std_uq32_32_from_quotient">from_quotient</a></code> and <code><a href="../std/uq32_32.md#std_uq32_32_from_int">from_int</a></code> should be preferred over using <code><a href="../std/uq32_32.md#std_uq32_32_from_raw">from_raw</a></code>. Unless the denominator is a power of two, fractions can not be represented accurately, so be careful about rounding errors. Aborts if the denominator is zero. Aborts if the input is non-zero but so small that it will be represented as zero, e.g. smaller than 2^{-32}. Aborts if the input is too large, e.g. larger than or equal to 2^32.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_from_quotient">from_quotient</a>(numerator: <a href="../std/u64.md#std_u64">u64</a>, denominator: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_from_quotient">from_quotient</a>(numerator: <a href="../std/u64.md#std_u64">u64</a>, denominator: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> { <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>( <a href="../std/macros.md#std_macros_uq_from_quotient">std::macros::uq_from_quotient</a>!<<a href="../std/u64.md#std_u64">u64</a>, <a href="../std/u128.md#std_u128">u128</a>>( numerator, denominator, <a href="../std/u64.md#std_u64_max_value">std::u64::max_value</a>!(), <a href="../std/uq32_32.md#std_uq32_32_TOTAL_BITS">TOTAL_BITS</a>, <a href="../std/uq32_32.md#std_uq32_32_FRACTIONAL_BITS">FRACTIONAL_BITS</a>, <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EDenominator">EDenominator</a>, <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EQuotientTooSmall">EQuotientTooSmall</a>, <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EQuotientTooLarge">EQuotientTooLarge</a>, ), ) } </code></pre> </details><a name="std_uq32_32_from_int"></a>
from_intCreate a fixed-point value from an integer. <code><a href="../std/uq32_32.md#std_uq32_32_from_int">from_int</a></code> and <code><a href="../std/uq32_32.md#std_uq32_32_from_quotient">from_quotient</a></code> should be preferred over using <code><a href="../std/uq32_32.md#std_uq32_32_from_raw">from_raw</a></code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_from_int">from_int</a>(integer: <a href="../std/u32.md#std_u32">u32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_from_int">from_int</a>(integer: <a href="../std/u32.md#std_u32">u32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> { <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>(<a href="../std/macros.md#std_macros_uq_from_int">std::macros::uq_from_int</a>!(integer, <a href="../std/uq32_32.md#std_uq32_32_FRACTIONAL_BITS">FRACTIONAL_BITS</a>)) } </code></pre> </details><a name="std_uq32_32_add"></a>
addAdd two fixed-point numbers, <code>a + b</code>. Aborts if the sum overflows.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_add">add</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_add">add</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> { <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>(<a href="../std/macros.md#std_macros_uq_add">std::macros::uq_add</a>!<<a href="../std/u64.md#std_u64">u64</a>, <a href="../std/u128.md#std_u128">u128</a>>(a.0, b.0, <a href="../std/u64.md#std_u64_max_value">std::u64::max_value</a>!(), <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EOverflow">EOverflow</a>)) } </code></pre> </details><a name="std_uq32_32_sub"></a>
subSubtract two fixed-point numbers, <code>a - b</code>. Aborts if <code>a < b</code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_sub">sub</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_sub">sub</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> { <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>(<a href="../std/macros.md#std_macros_uq_sub">std::macros::uq_sub</a>!(a.0, b.0, <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EOverflow">EOverflow</a>)) } </code></pre> </details><a name="std_uq32_32_mul"></a>
mulMultiply two fixed-point numbers, truncating any fractional part of the product. Aborts if the product overflows.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_mul">mul</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_mul">mul</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> { <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>(<a href="../std/uq32_32.md#std_uq32_32_int_mul">int_mul</a>(a.0, b)) } </code></pre> </details><a name="std_uq32_32_div"></a>
divDivide two fixed-point numbers, truncating any fractional part of the quotient. Aborts if the divisor is zero. Aborts if the quotient overflows.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_div">div</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_div">div</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> { <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>(<a href="../std/uq32_32.md#std_uq32_32_int_div">int_div</a>(a.0, b)) } </code></pre> </details><a name="std_uq32_32_to_int"></a>
to_intConvert a fixed-point number to an integer, truncating any fractional part.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_to_int">to_int</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/u32.md#std_u32">u32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_to_int">to_int</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/u32.md#std_u32">u32</a> { <a href="../std/macros.md#std_macros_uq_to_int">std::macros::uq_to_int</a>!(a.0, <a href="../std/uq32_32.md#std_uq32_32_FRACTIONAL_BITS">FRACTIONAL_BITS</a>) } </code></pre> </details><a name="std_uq32_32_int_mul"></a>
int_mulMultiply a <code><a href="../std/u64.md#std_u64">u64</a></code> integer by a fixed-point number, truncating any fractional part of the product. Aborts if the product overflows.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_int_mul">int_mul</a>(val: <a href="../std/u64.md#std_u64">u64</a>, multiplier: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/u64.md#std_u64">u64</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_int_mul">int_mul</a>(val: <a href="../std/u64.md#std_u64">u64</a>, multiplier: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/u64.md#std_u64">u64</a> { <a href="../std/macros.md#std_macros_uq_int_mul">std::macros::uq_int_mul</a>!<<a href="../std/u64.md#std_u64">u64</a>, <a href="../std/u128.md#std_u128">u128</a>>( val, multiplier.0, <a href="../std/u64.md#std_u64_max_value">std::u64::max_value</a>!(), <a href="../std/uq32_32.md#std_uq32_32_FRACTIONAL_BITS">FRACTIONAL_BITS</a>, <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EOverflow">EOverflow</a>, ) } </code></pre> </details><a name="std_uq32_32_int_div"></a>
int_divDivide a <code><a href="../std/u64.md#std_u64">u64</a></code> integer by a fixed-point number, truncating any fractional part of the quotient. Aborts if the divisor is zero. Aborts if the quotient overflows.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_int_div">int_div</a>(val: <a href="../std/u64.md#std_u64">u64</a>, divisor: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/u64.md#std_u64">u64</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_int_div">int_div</a>(val: <a href="../std/u64.md#std_u64">u64</a>, divisor: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/u64.md#std_u64">u64</a> { <a href="../std/macros.md#std_macros_uq_int_div">std::macros::uq_int_div</a>!<<a href="../std/u64.md#std_u64">u64</a>, <a href="../std/u128.md#std_u128">u128</a>>( val, divisor.0, <a href="../std/u64.md#std_u64_max_value">std::u64::max_value</a>!(), <a href="../std/uq32_32.md#std_uq32_32_FRACTIONAL_BITS">FRACTIONAL_BITS</a>, <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EDivisionByZero">EDivisionByZero</a>, <b>abort</b> <a href="../std/uq32_32.md#std_uq32_32_EOverflow">EOverflow</a>, ) } </code></pre> </details><a name="std_uq32_32_le"></a>
leLess than or equal to. Returns <code><b>true</b></code> if and only if <code>a <= b</code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_le">le</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_le">le</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> { a.0 <= b.0 } </code></pre> </details><a name="std_uq32_32_lt"></a>
ltLess than. Returns <code><b>true</b></code> if and only if <code>a < b</code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_lt">lt</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_lt">lt</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> { a.0 < b.0 } </code></pre> </details><a name="std_uq32_32_ge"></a>
geGreater than or equal to. Returns <code><b>true</b></code> if and only if <code>a >= b</code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_ge">ge</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_ge">ge</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> { a.0 >= b.0 } </code></pre> </details><a name="std_uq32_32_gt"></a>
gtGreater than. Returns <code><b>true</b></code> if and only if <code>a > b</code>.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_gt">gt</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_gt">gt</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>, b: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/bool.md#std_bool">bool</a> { a.0 > b.0 } </code></pre> </details><a name="std_uq32_32_to_raw"></a>
to_rawAccessor for the raw u64 value. Can be paired with <code><a href="../std/uq32_32.md#std_uq32_32_from_raw">from_raw</a></code> to perform less common operations on the raw values directly.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_to_raw">to_raw</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a>): <a href="../std/u64.md#std_u64">u64</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_to_raw">to_raw</a>(a: <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>): <a href="../std/u64.md#std_u64">u64</a> { a.0 } </code></pre> </details><a name="std_uq32_32_from_raw"></a>
from_rawAccessor for the raw u64 value. Can be paired with <code><a href="../std/uq32_32.md#std_uq32_32_to_raw">to_raw</a></code> to perform less common operations on the raw values directly.
<pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_from_raw">from_raw</a>(raw_value: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">std::uq32_32::UQ32_32</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../std/uq32_32.md#std_uq32_32_from_raw">from_raw</a>(raw_value: <a href="../std/u64.md#std_u64">u64</a>): <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a> { <a href="../std/uq32_32.md#std_uq32_32_UQ32_32">UQ32_32</a>(raw_value) } </code></pre> </details>