SSerguey Asael Shinder
Java coding notes: the JVM, and writing software that lasts

Serguey Asael Shinder: Assigning a long to a double is a widening conversion that can drop the last digit

· by Serguey Asael Shinder / Serguey Shinder

This week the maintainers of xrpl.js, the JavaScript library for the XRP Ledger, had to deal with a precision bug that dated back three major versions. In pull request 3341 they explain that dropsToXrp('9999999999999999') returned 9999999999.999998, because a JavaScript number is a 64-bit double and cannot hold every drop once balances pass about 8.6 billion XRP. The fix changes the return type to BigNumber, which is a breaking change, so on 12 November it was reverted on main and moved to a 6.0.0 branch.

Java developers tend to think this cannot happen to them, because the amount would be a long. It can, and the language does it without a cast.

The Java Language Specification, section 5.1.2 lists long to double among the widening primitive conversions, the ones the compiler applies silently in assignments and arithmetic. It also says what happens to the value. Conversions from int to double, or between integral types, preserve the value exactly. A conversion from int to float, or from long to float, or from long to double, "may result in loss of precision", meaning the least significant bits can be lost, and the result is the integer correctly rounded using round-to-nearest. The same section adds that despite the possible loss, a widening conversion "never results in a run-time exception".

Serguey Asael Shinder: Assigning a long to a double is a widening conversion that can drop the last digit
Assigning a long to a double is a widening conversion that can drop the last digit — Serguey Asael Shinder

A double has 53 bits of significand, so every long up to 2^53 converts exactly and some above it do not:

long amount = 9_007_199_254_740_993L;   // 2^53 + 1
double d = amount;                      // widening, no cast, no warning
long back = (long) d;                   // 9_007_199_254_740_992
assert back != amount;                  // one unit gone

No exception, and no compiler error, because the specification defines this as a legal widening. The JLS even includes its own example of the effect for int to float, where 1234567890 comes back 46 short.

The places this bites are the ones where a long is quietly passed to something that takes a double: a Math method, a metrics library, a JSON serialiser that writes numbers as doubles, or a ratio computed by dividing two amounts. The xrpl.js bug is the same pattern one step later, when the integer is converted to a fractional unit.

For money, keep the integer as long as possible and convert to a decimal type exactly when you need a fractional value. BigDecimal.valueOf(long unscaledVal, int scale) does that without passing through a double: an amount of drops with scale 6 gives the XRP value with every digit intact. And where a conversion to double is genuinely needed, check whether the value is above 2^53 first, because the language will not check it for you.