Is there any generic algorithm which implements a floating point radix conversion?

Lets say we have a $p$-digit FP number

$A = \sum_{i=0}^{p-1} A_i \beta^{e-i}$

in radix $\beta$ and with $0 \leq A_i < \beta$.

How do we find the $A'_i$, $e'$ values for the $p'$-digit base $\gamma$ FP number

$A' = \sum_{i=0}^{p'-1} A'_i \gamma^{e'-i}$

closest to $A$?

There is one question which explicitly asks about radix 2 to radix 10 conversion, but unfortunately the answers seem to be specific for these radix combination. Here I ask about the general case.

Also is an intermediate arbitrary precision FP calculation really necessary? (as in the function strtod in David Gay's dtoa.c)

  • $\begingroup$ Except $A$ and $A'$, do all other variables taking integer values only? Are $p'$ and $\gamma$ given? $\endgroup$
    – John L.
    Nov 20 '18 at 12:22

In the worst case, you may need very high precision intermediate results. If your problem is limited, for example p ≤ 50 and e ≤ 1,000, it will be possible to prove how much precision is needed at most - with $1000 \cdot 10^{50}$ possible values to convert, 200 bits precision are likely enough.

In practice, some values can be calculated with correct rounding by calculating them with a single floating point operation with exact operands. Many other values can be calculated rounded correctly (including proof) with just a bit of extra precision. So you can do the calculation with relatively few extra bits of precision, check if you have a proven correctly rounded result, and do the same calculation with more bits if needed.

Example: 10.00073 = 1,000,073 / 100,000 Is the quotient of two exact numbers and therefore correctly rounded. 1e30 = 1e15 x 1e15 = product of two exact numbers. 1,000,000,000,000,000,123,456,789,012,345 = FMA (1e15,1e15, 123,456,789,012,344) = fused multiply-add of there exact numbers.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.