# 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:

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

## 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 Value | Enclosing Number | Representation |

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

## 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 API | What it does |

float_set | Create a float from an exponent and mantissa |

float_multiply | Multiply two XFL numbers together |

float_mulratio | Multiply an XFL floating point by a non-XFL numerator and denominator |

float_negate | Negate an XFL floating point number |

float_compare | Perform a comparison on two XFL floating point numbers |

float_sum | Add two XFL numbers together |

float_sto | Output an XFL as a serialized object |

float_sto_set | Read a serialized amount into an XFL |

float_invert | Divide one by an XFL floating point number |

float_divide | Divide an XFL by another XFL floating point number |

float_one | Return the number 1 represented in an XFL enclosing number |

float_exponent | Get the exponent of an XFL enclosing number |

float_mantissa | Get the mantissa of an XFL enclosing number |

float_sign | 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 |

float_int | Convert an XFL floating point into an integer (floor) |

float_root | Compute the nth root of an XFL |

float_log | 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 exceptionis 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.

Updated 3 months ago

**Whatβs Next**