New floating-to-decimal formatting routine by lifthrasiir · Pull Request #24612 · rust-lang/rust (original) (raw)

Probably I've introduced the concepts backwards.

The fixed mode can be implemented via the exact mode and the estimator, which calculates the required number of significant digits from the number of fractional digits and the original value. It turns out that the internal digit generation code already has to estimate the exponent (which is almost same to the difference between them), so we can reuse this logic.

In order to achieve this merger, the combined exact-fixed mode is implemented so that both the number of significant digits len and the number of fractional digits frac_digits are given. (The actual interface differs from this to be more flexible: len == buf.len() and frac_digits == -limit. This doesn't change the discussion however.) The digit generation code will finish whichever comes first, so it is possible that there are no digits generated. The n as I originally described is really determined from either len or frac_digits, so it might have been confusing.

By the way, I realized that my original example doesn't arise with the current external interface. So I'll give a better example: v = 0.000045, len = 3, frac_digits = 3 (so that limit = -3). Since v = 0.45 * 10^-4, k = -4, but this clearly triggers the condition k <= limit. This means that the resulting representation is all zeroes, i.e. V = 0.000 (note that k is unused here).

(The better description for this is always welcomed. 😭)