High precision calculations are native to Hooks.
Floating point numbers are widely used in computer science to do calculation of finite precision but arbitrary scale numbers.
Most modern CPUs are capable of performing fast floating point operations using the IEEE binary floating point standard however xahaud
does not use this format. Instead Xahau uses a bespoke decimal floating point standard.
This custom format has three basic properties:
The format is inherently decimal, expressed as a decimal mantissa
multipled by 10
to the power of an exponent
.
All values expressed have 16 significant (decimal) figures.
The range of exponents is -96
to +80
When serialized the mantissa is 54 bits, and the exponent is 8 bits, with a final sign bit bringing the total size of the serialized floating point to 63 bits.
XLS-17d is an XRPL standards proposal that defines an efficient way to pack and store xrpld floating point numbers (as described above).
XFLs store the bits of the floating point number within an enclosing number. This is always an int64_t
. Negative enclosing numbers represent invalid XFLs (for example as a result of division by zero.)
📘 Hint
Use the XFL-tool here to compose and decompose XFLs in your browser!
Some example XFLs follow
-1
1478180677777522688
-1000000000000000 * 10^(-15)
0
0
0 (canonical zero)
1
6089866696204910592
+1000000000000000 * 10^(-15)
PI
6092008288858500385
+3141592653589793 * 10^(-15)
-PI
1480322270431112481
-3141592653589793 * 10^(-15)
This format is very convenient for Hooks, as Hooks can only exchange integer values with xrpld. By enclosing the floating point inside an integer in a well defined way it becomes possible to do complex floating point computations from a Hook. This is useful for computing exchange rates.
Floating point regimes typically have a number of different ways to express zero, which can be a problem for testing for zero. For example 0 x 10 ^ 1
is zero and 0 x 10 ^ 2
is also zero. For this reason there is a canonical zero enforced by the standard and the Hook API. The canonical zero is also enclosing number zero (0
).
Once you have an XFL you can use the Float API to do various computations. The Float API appears in the table below. Each API takes one or more XFL enclosing numbers and returns an XFL enclosing number. Negative return values always represent a computational error (such as division by zero). There are no valid negative enclosing numbers.
Create a float from an exponent and mantissa
Multiply two XFL numbers together
Multiply an XFL floating point by a non-XFL numerator and denominator
Negate an XFL floating point number
Perform a comparison on two XFL floating point numbers
Add two XFL numbers together
Output an XFL as a serialized object
Read a serialized amount into an XFL
Divide one by an XFL floating point number
Divide an XFL by another XFL floating point number
Return the number 1 represented in an XFL enclosing number
Get the exponent of an XFL enclosing number
Get the mantissa of an XFL enclosing number
Get the sign of an XFL enclosing number
float_exponent_set
Set the exponent of an XFL enclosing number
float_mantissa_set
Set the mantissa of an XFL enclosing number
float_sign_set
Set the sign of an XFL enclosing number
Convert an XFL floating point into an integer (floor)
Compute the nth root of an XFL
Compute the decimal log of an XFL
❗️ Warning
You should never do any direct math or comparison on the enclosing number. This will almost always result in incorrect computations.
The sole exception is checking for canonical zero.
In the below example an exchange rate conversion is performed, followed by a high precision fraction multiplication.
🚧 Tip
If a float API returns a negative value and you do no check for negatives then feeding that negative value into another float API will also produce a negative value. In this way errors are propagated much as
NaN
(not a number) is propagated in other languages.If you ever end up with a negative enclosing number an error occured somewhere in your floating point calculations.