Defines a fixed-point numeric type with a 32-bit integer part and a 32-bit fractional part.
- Struct
FixedPoint32
- Constants
- Function
multiply_u64
- Function
divide_u64
- Function
create_from_rational
- Function
create_from_raw_value
- Function
get_raw_value
- Function
is_zero
- Function
min
- Function
max
- Function
create_from_u64
- Function
floor
- Function
ceil
- Function
round
- Module Specification
Define a fixed-point numeric type with 32 fractional bits. This is just a u64 integer but it is wrapped in a struct to make a unique type. 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). For comparison, double precision floating-point has less than 16 decimal digits of precision, so be careful about using floating-point to convert these values to decimal.
struct FixedPoint32 has copy, drop, store
The denominator provided was zero
const EDENOMINATOR: u64 = 65537;
The quotient value would be too large to be held in a u64
A division by zero was encountered
const EDIVISION_BY_ZERO: u64 = 65540;
The multiplied value would be too large to be held in a u64
const EMULTIPLICATION: u64 = 131075;
The computed ratio when converting to a FixedPoint32
would be unrepresentable
const ERATIO_OUT_OF_RANGE: u64 = 131077;
Multiply a u64 integer by a fixed-point number, truncating any fractional part of the product. This will abort if the product overflows.
public fun multiply_u64(val: u64, multiplier: fixed_point32::FixedPoint32): u64
Divide a u64 integer by a fixed-point number, truncating any fractional part of the quotient. This will abort if the divisor is zero or if the quotient overflows.
public fun divide_u64(val: u64, divisor: fixed_point32::FixedPoint32): u64
Create a fixed-point value from a rational number specified by its
numerator and denominator. Calling this function should be preferred
for using Self::create_from_raw_value
which is also available.
This will abort if the denominator is zero. It will also
abort if the numerator is nonzero and the ratio is not in the range
2^-32 .. 2^32-1. When specifying decimal fractions, be careful about
rounding errors: if you round to display N digits after the decimal
point, you can use a denominator of 10^N to avoid numbers where the
very small imprecision in the binary representation could change the
rounding, e.g., 0.0125 will round down to 0.012 instead of up to 0.013.
public fun create_from_rational(numerator: u64, denominator: u64): fixed_point32::FixedPoint32
Create a fixedpoint value from a raw value.
public fun create_from_raw_value(value: u64): fixed_point32::FixedPoint32
Accessor for the raw u64 value. Other less common operations, such as adding or subtracting FixedPoint32 values, can be done using the raw values directly.
public fun get_raw_value(num: fixed_point32::FixedPoint32): u64
Returns true if the ratio is zero.
public fun is_zero(num: fixed_point32::FixedPoint32): bool
Returns the smaller of the two FixedPoint32 numbers.
public fun min(num1: fixed_point32::FixedPoint32, num2: fixed_point32::FixedPoint32): fixed_point32::FixedPoint32
Returns the larger of the two FixedPoint32 numbers.
public fun max(num1: fixed_point32::FixedPoint32, num2: fixed_point32::FixedPoint32): fixed_point32::FixedPoint32
Create a fixedpoint value from a u64 value.
public fun create_from_u64(val: u64): fixed_point32::FixedPoint32
Returns the largest integer less than or equal to a given number.
public fun floor(num: fixed_point32::FixedPoint32): u64
Rounds up the given FixedPoint32 to the next largest integer.
public fun ceil(num: fixed_point32::FixedPoint32): u64
Returns the value of a FixedPoint32 to the nearest integer.
public fun round(num: fixed_point32::FixedPoint32): u64