next up previous contents
Next: Signed Binary to Decimal Up: Decimal to Signed Binary Previous: Decimal integers to signed

Decimal fractions to signed binary

Suppose we wish to compute the signed binary representation U of the decimal fraction $(0.u_{-1}u_{-2}u_{-3} \dots u_{-m})$ using signed binary operations, we simply evaluate

\begin{displaymath}
U = u_{-1}\!\cdot\!10^{-1} + u_{-2}\!\cdot\!10^{-2} + u_{-3}\!\cdot\!10^{-3} + \cdots + u_{-m}\!\cdot\!10^{-m}\end{displaymath}

To compute this more efficiently, we observe

\begin{displaymath}
U = (((u_{-m}/10 + u_{-(m-1)})/10 + \cdots + u_{-2})/10 +u_{-1})/10\end{displaymath}

We can now use a simple recursive algorithm using the average and division by an integer operations which evaluates to zero on an empty input, but otherwise computes:

\begin{displaymath}
\mathrm{decSB\_frac}(a::x) = \frac{a + \mathrm{decSB\_frac}(x)}{10} = \frac{a \oplus \mathrm{decSB\_frac}(x)}{5}\end{displaymath}

Because we cannot represent decimal digits in signed binary streams, we replace a in the right hand side of the expression by a stream which represents a/24, and multiply the entire result by 24. Multiplication and division by powers of two can be performed easily using shift operations.



Martin Escardo
5/11/2000