crates/sui-framework/docs/std/macros.md
This module holds shared implementation of macros used in <code>std</code>
num_maxnum_minnum_diffnum_divide_and_round_upnum_pownum_sqrtnum_to_stringnum_checked_addnum_checked_subnum_checked_mulnum_checked_divnum_saturating_addnum_saturating_subnum_saturating_mulnum_checked_shlnum_checked_shrnum_lossless_shlnum_lossless_shrnum_lossless_divrange_dorange_do_eqdodo_eqtry_as_u8try_as_u16try_as_u32try_as_u64try_as_u128uq_from_quotientuq_from_intuq_adduq_subuq_to_intuq_int_muluq_int_div<a name="std_macros_num_max"></a>
num_max<a name="std_macros_num_min"></a>
num_min<a name="std_macros_num_diff"></a>
num_diff<a name="std_macros_num_divide_and_round_up"></a>
num_divide_and_round_up<a name="std_macros_num_pow"></a>
num_pow<a name="std_macros_num_sqrt"></a>
num_sqrt<a name="std_macros_num_to_string"></a>
num_to_string<a name="std_macros_num_checked_add"></a>
num_checked_add<a name="std_macros_num_checked_sub"></a>
num_checked_sub<a name="std_macros_num_checked_mul"></a>
num_checked_mul<a name="std_macros_num_checked_div"></a>
num_checked_div<a name="std_macros_num_saturating_add"></a>
num_saturating_add<a name="std_macros_num_saturating_sub"></a>
num_saturating_sub<a name="std_macros_num_saturating_mul"></a>
num_saturating_mul<a name="std_macros_num_checked_shl"></a>
num_checked_shl<a name="std_macros_num_checked_shr"></a>
num_checked_shr<a name="std_macros_num_lossless_shl"></a>
num_lossless_shl<a name="std_macros_num_lossless_shr"></a>
num_lossless_shr<a name="std_macros_num_lossless_div"></a>
num_lossless_div<a name="std_macros_range_do"></a>
range_do<a name="std_macros_range_do_eq"></a>
range_do_eq<a name="std_macros_do"></a>
do<a name="std_macros_do_eq"></a>
do_eq<a name="std_macros_try_as_u8"></a>
try_as_u8<a name="std_macros_try_as_u16"></a>
try_as_u16<a name="std_macros_try_as_u32"></a>
try_as_u32<a name="std_macros_try_as_u64"></a>
try_as_u64<a name="std_macros_try_as_u128"></a>
try_as_u128<a name="std_macros_uq_from_quotient"></a>
uq_from_quotientCreates a fixed-point value from a quotient specified by its numerator and denominator. <code>$T</code> is the underlying integer type for the fixed-point value, where <code>$T</code> has <code>$t_bits</code> bits. <code>$U</code> is the type used for intermediate calculations, where <code>$U</code> is the next larger integer type. <code>$max_t</code> is the maximum value that can be represented by <code>$T</code>. <code>$t_bits</code> (as mentioned above) is the total number of bits in the fixed-point value (integer plus fractional). <code>$fractional_bits</code> is the number of fractional bits in the fixed-point value.
<pre><code><b>public</b>(package) <b>macro</b> <b>fun</b> <a href="../std/macros.md#std_macros_uq_from_quotient">uq_from_quotient</a><$T, $U>($numerator: $T, $denominator: $T, $max_t: $T, $t_bits: <a href="../std/u8.md#std_u8">u8</a>, $fractional_bits: <a href="../std/u8.md#std_u8">u8</a>, $abort_denominator: _, $abort_quotient_too_small: _, $abort_quotient_too_large: _): $T </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>macro</b> <b>fun</b> <a href="../std/macros.md#std_macros_uq_from_quotient">uq_from_quotient</a><$T, $U>( $numerator: $T, $denominator: $T, $max_t: $T, $t_bits: <a href="../std/u8.md#std_u8">u8</a>, $fractional_bits: <a href="../std/u8.md#std_u8">u8</a>, $abort_denominator: _, $abort_quotient_too_small: _, $abort_quotient_too_large: _, ): $T { <b>let</b> numerator = $numerator; <b>let</b> denominator = $denominator; <b>if</b> (denominator == 0) $abort_denominator; // Scale the numerator to have `$t_bits` fractional bits and the denominator to have // `$t_bits - $fractional_bits` fractional bits, so that the quotient will have // `$fractional_bits` fractional bits. <b>let</b> scaled_numerator = numerator <b>as</b> $U << $t_bits; <b>let</b> scaled_denominator = denominator <b>as</b> $U << ($t_bits - $fractional_bits); <b>let</b> quotient = scaled_numerator / scaled_denominator; // The quotient can only be zero <b>if</b> the numerator is also zero. <b>if</b> (quotient == 0 && numerator != 0) $abort_quotient_too_small; // Return the quotient <b>as</b> a fixed-point number. We first need to check whether the cast // can succeed. <b>if</b> (quotient > $max_t <b>as</b> $U) $abort_quotient_too_large; quotient <b>as</b> $T } </code></pre> </details><a name="std_macros_uq_from_int"></a>
uq_from_int<a name="std_macros_uq_add"></a>
uq_add<a name="std_macros_uq_sub"></a>
uq_sub<a name="std_macros_uq_to_int"></a>
uq_to_int<a name="std_macros_uq_int_mul"></a>
uq_int_mul<a name="std_macros_uq_int_div"></a>
uq_int_div