0017 XLS-17d: XFL Developer-friendly representation of XRPL balances #39
Replies: 4 comments 2 replies
-
Is it safe to assume that this new format will have to come with its own dedicated math library? |
Beta Was this translation helpful? Give feedback.
-
There is a small mismatch with the reference implementation. The line
should be the other way around |
Beta Was this translation helpful? Give feedback.
-
I propose a minor change in the binary representation in order to make XFLs sortable by their casted 64 bit integer value:
This will allow for ultra fast sorting using any int64 capable sorting algorithm and will make the default logical operators work out of the box. A working Javascript implementation of this change can be found in the xrplkit/xfl library |
Beta Was this translation helpful? Give feedback.
-
As discussed some months ago with @Mwni my preferred solution to the problem of sort-ability is to introduce packed vs unpacked XFLs. Normal XFLs are considered packed, and are not sortable. However unpacking them into a sortable format is very inexpensive: int64_t unpack_xfl(int64_t packed)
{
if (is_negative(packed))
return ~packed;
return packed;
}
int64_t pack_xfl(int64_t unpacked)
{
if (unpacked < 0)
return ~unpacked;
return unpacked;
} |
Beta Was this translation helpful? Give feedback.
-
Background
The XRP ledger allows for two types of balances on accounts:
xrp
balancestrustline
balancesNative balances are encoded and processed as signed 63bit integer values with an implicit decimal point at the 6th place from the right. A single unit is referred to as a drop. Thus the smallest possible value is
1 drop
represented logically as:0.000001 xrp
.Trustline balances are encoded and processed as a 63 bit decimal floating point number in the following format:
{
sign bit
8 bits of exponent
54 bits of mantissa
}Note: This is not the IEEE floating point format (which is base 2.)
0b00000000
is10^(-97)
.10^15
and10^16 - 1
A 64th disambiguation bit is prepended (most significant bit) to both datatypes, which is
0
in the case of a native balance and1
in the case of a trustline balance. [1]Problem Definition
Performing computations between these two number formats can be difficult and error-prone for third party developers.
One existing partial solution is passing these amounts around as human-readable decimal numbers encoded as strings. This is computationally intensive and still does not allow numbers of one type to be mathematically operated on with numbers of the other type in a consistent and predictable way.
Internally the XRPL requires two independent types, however any xrp balance less than
10B
will translate without loss of precision into the trustline balance type. For amounts of xrp above10B
(but less than100B
) only 1 significant figure is lost from the least significant side. Thus in the worst possible case (moving >=10B
XRP)10
drops may be lost from an amount.The benefits of representing xrp balances in the trustline balance format are:
XFL Format
The XFL format is designed for ease of use and maximum compatibility across a wide variety of processors and platforms.
For maximum ease of passing and returning from (pure) functions all XFL numbers are encoded into an
enclosing number
which is always a signed 64 bit integer, as follows:Note: bit 63 is the most significant bit
enclosing sign bit
always 0 for a valid XFLinternal sign bit
0 = negative 1 = positive. note: this is not the int64_t's sign bit.exponent
biased such that0b000000000
= -97mantissa
between10^15
and10^16 - 1
Special case:
Any XFL with a negative enclosing sign bit is
invalid
. This DOES NOT refer to the internal sign bit inside the XFL format. It is definitely possible to have a negative value represented in an XFL, however these always exist with a POSITIVE enclosing sign bit.Invalid (negative enclosing sign bit) XFL values are reserved for propagation of error codes. If an invalid XFL is passed to an XFL processing function (for example
float_multiply
) it too should return an invalid XFL.Examples
Reference Implementations
Javascript:
C:
Beta Was this translation helpful? Give feedback.
All reactions