Floating Point Numbers (XFL)

High precision calculations are native to Hooks.

Background

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 xrpld does not use this format. Instead the XRP Ledger 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.

What is XFL?

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

Floating 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.

Canonical Zero

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).

Hook Float API

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

โ—๏ธ

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.

Example

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);

๐Ÿšง

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.


Whatโ€™s Next