Skip to content

Floating Point Numbers (XFL)

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:

  1. The format is inherently decimal, expressed as a decimal mantissa multipled by 10 to the power of an exponent.
  2. All values expressed have 16 significant (decimal) figures.
  3. 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.)

Some example XFLs follow

loating Point ValueEnclosing NumberRepresentation
-11478180677777522688-1000000000000000 * 10^(-15)
000 (canonical zero)
16089866696204910592+1000000000000000 * 10^(-15)
PI6092008288858500385+3141592653589793 * 10^(-15)
-PI1480322270431112481-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.

Hook APIWhat it does
float_setCreate a float from an exponent and mantissa
float_multiplyMultiply two XFL numbers together
float_mulratioMultiply an XFL floating point by a non-XFL numerator and denominator
float_negateNegate an XFL floating point number
float_comparePerform a comparison on two XFL floating point numbers
float_sumAdd two XFL numbers together
float_stoOutput an XFL as a serialized object
float_sto_setRead a serialized amount into an XFL
float_invertDivide one by an XFL floating point number
float_divideDivide an XFL by another XFL floating point number
float_oneReturn the number 1 represented in an XFL enclosing number
float_exponentGet the exponent of an XFL enclosing number
float_mantissaGet the mantissa of an XFL enclosing number
float_signGet the sign of an XFL enclosing number
float_exponent_setSet the exponent of an XFL enclosing number
float_mantissa_setSet the mantissa of an XFL enclosing number
float_sign_setSet the sign of an XFL enclosing number
float_intConvert an XFL floating point into an integer (floor)
float_rootCompute the nth root of an XFL
float_logCompute the decimal log of an XFL

In the below example an exchange rate conversion is performed, followed by a high precision fraction multiplication.

int64_t max_vault_pusd =
float_multiply(vault_xrp, exchange_rate);
max_vault_pusd =
float_mulratio(max_vault_pusd, 0,
NEW_COLLATERALIZATION_NUMERATOR, NEW_COLLATERALIZATION_DENOMINATOR);