Skip to main content

Module std::uq32_32

Defines an unsigned, fixed-point numeric type with a 32-bit integer part and a 32-bit fractional part. The notation uq32_32 and UQ32_32 is based on Q notation. q indicates it a fixed-point number. The u prefix indicates it is unsigned. The 32_32 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.

Struct UQ32_32

A 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).

public struct UQ32_32 has copy, drop, store
Click to open
Fields
0: u64

Constants

#[error]
const EDenominator: vector<u8> = b"Quotient specified with a zero denominator";
#[error]
const EQuotientTooSmall: vector<u8> = b"Quotient specified is too small, and is outside of the supported range";
#[error]
const EQuotientTooLarge: vector<u8> = b"Quotient specified is too large, and is outside of the supported range";
#[error]
const EOverflow: vector<u8> = b"Overflow from an arithmetic operation";
#[error]
const EDivisionByZero: vector<u8> = b"Division by zero";

The total number of bits in the fixed-point number. Used in macro invocations.

const TOTAL_BITS: u8 = 64;

The number of fractional bits in the fixed-point number. Used in macro invocations.

const FRACTIONAL_BITS: u8 = 32;

Function from_quotient

Create a fixed-point value from a quotient specified by its numerator and denominator. from_quotient and from_int should be preferred over using from_raw. 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.

public fun from_quotient(numerator: u64, denominator: u64): std::uq32_32::UQ32_32

Function from_int

Create a fixed-point value from an integer. from_int and from_quotient should be preferred over using from_raw.

public fun from_int(integer: u32): std::uq32_32::UQ32_32

Function add

Add two fixed-point numbers, a + b.
Aborts if the sum overflows.

public fun add(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): std::uq32_32::UQ32_32

Function sub

Subtract two fixed-point numbers, a - b.
Aborts if a < b.

public fun sub(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): std::uq32_32::UQ32_32

Function mul

Multiply two fixed-point numbers, truncating any fractional part of the product.
Aborts if the product overflows.

public fun mul(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): std::uq32_32::UQ32_32

Function div

Divide two fixed-point numbers, truncating any fractional part of the quotient.
Aborts if the divisor is zero.
Aborts if the quotient overflows.

public fun div(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): std::uq32_32::UQ32_32

Function to_int

Convert a fixed-point number to an integer, truncating any fractional part.

public fun to_int(a: std::uq32_32::UQ32_32): u32

Function int_mul

Multiply a u64 integer by a fixed-point number, truncating any fractional part of the product.
Aborts if the product overflows.

public fun int_mul(val: u64, multiplier: std::uq32_32::UQ32_32): u64

Function int_div

Divide a u64 integer by a fixed-point number, truncating any fractional part of the quotient.
Aborts if the divisor is zero.
Aborts if the quotient overflows.

public fun int_div(val: u64, divisor: std::uq32_32::UQ32_32): u64

Function le

Less than or equal to. Returns true if and only if a <= b.

public fun le(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): bool

Function lt

Less than. Returns true if and only if a < b.

public fun lt(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): bool

Function ge

Greater than or equal to. Returns true if and only if a >= b.

public fun ge(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): bool

Function gt

Greater than. Returns true if and only if a > b.

public fun gt(a: std::uq32_32::UQ32_32, b: std::uq32_32::UQ32_32): bool

Function to_raw

Accessor for the raw u64 value. Can be paired with from_raw to perform less common operations on the raw values directly.

public fun to_raw(a: std::uq32_32::UQ32_32): u64

Function from_raw

Accessor for the raw u64 value. Can be paired with to_raw to perform less common operations on the raw values directly.

public fun from_raw(raw_value: u64): std::uq32_32::UQ32_32