# algorithm for correctly rounded floating point radix conversion

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)

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

## 1 Answer

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