FixedPoint64
ferum_std::fixed_point_64
Ferum's implementation of a FixedPoint number. Has fixed decimal places of 10 and a max value of MAX_U64 (18446744073709551615)
.
Operations that result in an overflow will error out.
Quick Example
Struct FixedPoint64
FixedPoint64
Fixedpoint struct. Can be stored, copied, and dropped.
Constants
DECIMAL_PLACES
Number of decimal places in a FixedPoint value.
ERR_EXCEED_MAX
Thrown when the value of a FixedPoint64 exceeds the max value able to be represented.
ERR_EXCEED_MAX_DECIMALS
Thrown when max decimals of FixedPoint64 is exceeded. Possible examples:
if trying to create a FixedPoint64 from a 12 decimal number
if multiplying two 6 decimal numbers together
ERR_EXCEED_MAX_EXP
Because move doesn't have a native power function, we need to hardcode powers of 10. Thrown if we try to get a power of 10 that is not hardcoded.
ERR_PRECISION_LOSS
Thrown when decimals are lost and not truncating or rounding up. Possible examples:
calling
to_u64
()
to convert a number that has 6 decimal places into 5 decimal places, losing a digitDividing a number with 10 decimal places by 0.01, exceeding the max decimal places FixedPoint64 can represent.
MAX_VALUE
Max value a FixedPoint can represent.
Functions
Function new_u64
new_u64
Create a new FixedPoint from a u64 value. No conversion is performed. Example: new_u64
(12345) == 0.0000012345
Function new_u128
new_u128
Create a new FixedPoint from a u128 value. No conversion is performed. Example: new_u128
(12345) == 0.0000012345
Function value
value
Returns the underlying value of the FixedPoint.
Function zero
zero
Return a FixedPoint that equals 0.
Function one
one
Return a FixedPoint that equals 1.
Function half
half
Return a FixedPoint that equals 0.5.
Function max_fp
max_fp
Returns the max FixedPoint value.
Function min_fp
min_fp
Returns the min FixedPoint value.
Function trunc_to_decimals
trunc_to_decimals
Returns a FixedPoint truncated to the given decimal places.
Function round_up_to_decimals
round_up_to_decimals
Returns a FixedPoint rounded up to the given decimal places.
Function to_u64_trunc
to_u64_trunc
Converts the FixedPoint to a u64 value with the given number of decimal places. Truncates any digits that are lost.
Function to_u128_trunc
to_u128_trunc
Converts the FixedPoint to a u128 value with the given number of decimal places. Truncates any digits that are lost.
Function to_u64_round_up
to_u64_round_up
Converts the FixedPoint to a u64 value with the given number of decimal places. Rounds up if digits are lost.
Function to_u128_round_up
to_u128_round_up
Converts the FixedPoint to a u128 value with the given number of decimal places. Rounds up if digits are lost.
Function to_u64
to_u64
Converts the FixedPoint to a u64 value with the given number of decimal places. Errors if any digits are lost.
Function to_u128
to_u128
Converts the FixedPoint to a u128 value with the given number of decimal places. Errors if any digits are lost.
Function from_u64
from_u64
Converts the value with the specified decimal places to a FixedPoint value.
Function from_u128
from_u128
Converts the value with the specified decimal places to a FixedPoint value.
Function multiply_trunc
multiply_trunc
Multiplies two FixedPoints, truncating if the number of decimal places exceeds DECIMAL_PLACES.
Function multiply_round_up
multiply_round_up
Multiplies two FixedPoints, rounding up if the number of decimal places exceeds DECIMAL_PLACES.
Function divide_trunc
divide_trunc
Divides two FixedPoints, truncating if the number of decimal places exceeds DECIMAL_PLACES.
Function divide_round_up
divide_round_up
Divides two FixedPoints, rounding up if the number of decimal places exceeds DECIMAL_PLACES.
Function sqrt_approx
sqrt_approx
Returns the approximation of the square root of the FixedPoint using the Babylonian method. The approximation will always be less then the actual square root.
Function add
add
Adds two FixedPoints.
Function sub
sub
Subtracts two FixedPoints.
Function lt
lt
Return true if a < b.
Function lte
lte
Return true if a <= b.
Function gt
gt
Return true if a > b.
Function gte
gte
Return true if a >= b.
Function eq
eq
Return true if a == b.
Function is_zero
is_zero
Return true if the value is zero.
Function max
max
Returns max(a, b).
Function min
min
Returns min(a, b).
Last updated