>>>>> "Richard" == Richard Fateman <fateman at cs.berkeley.edu> writes:
Richard> ;; return an integer that is the leading d digits of the floating point
Richard> number r
Richard> ;; not quite perfected; see below
Richard> (defun fp(r d)(round (* (rational r)(expt 10 (+ d (ceiling (log r 10)))))))
Richard> there is a need to watch at the margins for rounding bumping up the size of
Richard> a number:
Richard> that is, 0.990000 to 2 digits you multiply by 100 and get 99, correct.
Richard> but 0.999 to 2 digits you multiply by 100 and get 100, which has 3 decimal
Richard> digits. One too many.
This is probably good enough for bfloats. It looks much, much simpler
than the existing code. Still have to fix the corner cases, though.
Note also that there is another potential source of error caused by
roundoff in computing (log r 10).
Richard> I mention all this only because if you look at the
Richard> literature in binary to decimal conversion, the implicit
Richard> restriction on such programs is that you have to do it
Richard> using hardware arithmetic, whereas in common lisp, we
Richard> routinely have rational and arbitrary precision
Richard> arithmetic.
I think Burger and Dybvig actually implement a simple bignum package
that is just enough for their algorithm. (Their paper uses Scheme to
implement the algorithm, but you can get a C version from them.)
Ray