Skip to content

Latest commit

 

History

History
276 lines (135 loc) · 9.28 KB

fixed_point32.md

File metadata and controls

276 lines (135 loc) · 9.28 KB

Module 0x1::fixed_point32

Defines a fixed-point numeric type with a 32-bit integer part and a 32-bit fractional part.

Struct FixedPoint32

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

Constants

const MAX_U64: u128 = 18446744073709551615;

The denominator provided was zero

const EDENOMINATOR: u64 = 65537;

The quotient value would be too large to be held in a u64

const EDIVISION: u64 = 131074;

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;

Function multiply_u64

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

Function divide_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

Function create_from_rational

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

Function create_from_raw_value

Create a fixedpoint value from a raw value.

Function get_raw_value

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.

Function is_zero

Returns true if the ratio is zero.

public fun is_zero(num: fixed_point32::FixedPoint32): bool

Function min

Returns the smaller of the two FixedPoint32 numbers.

Function max

Returns the larger of the two FixedPoint32 numbers.

Function create_from_u64

Create a fixedpoint value from a u64 value.

Function floor

Returns the largest integer less than or equal to a given number.

Function ceil

Rounds up the given FixedPoint32 to the next largest integer.

Function round

Returns the value of a FixedPoint32 to the nearest integer.

Module Specification