fpprintprec



 
At the risk of slowing some of these things down, conversions and rounding
and such
can be done in common lisp with great economy of program length by using
rational
arithmetic.

Take any number, float or bfloat.
convert to a rational number.  There is a program in CL, rational, to do
this for floats.

To get d decimal digits of the rational number r you want as an integer in
base 10, you compute

(round (* r (expt 10 q)))

where q is appropriately chosen, e.g. as (+ d (ceiling (log r 10)).

putting them all together,

;; return an integer that is the leading d digits of the floating point
number r
;; not quite perfected; see below
(defun fp(r d)(round (* (rational r)(expt 10 (+ d (ceiling (log r 10)))))))

there is a need to watch at the margins for rounding bumping up the size of
a number:

that is, 0.990000  to 2 digits you multiply by 100 and get 99, correct.
but 0.999 to 2 digits you multiply by 100 and get 100,  which has 3 decimal
digits. One too many.

You can easily see if rounding has changed the number of digits... e.g. if
you asked for 3 digits
but the number is greater than 999,  you have this situation. 

I mention all this only because if you look at the literature in binary to
decimal conversion,
the implicit restriction on such programs is that you have to do it using
hardware arithmetic,
whereas in common lisp, we routinely have rational and arbitrary precision
arithmetic.

I have not looked at this in a very long time, but the  bfloat package can
do arithmetic in binary
or other bases (in particular, decimal), in an attempt to make this kind of
output calculation
faster. Speed is probably important anymore since other operations
surrounding output will dominate
arithmetic.

RJF




> -----Original Message-----
> From: maxima-bounces at math.utexas.edu 
> [mailto:maxima-bounces at math.utexas.edu] On Behalf Of Raymond Toy
> Sent: Wednesday, October 08, 2008 8:00 AM
> To: Barton Willis
> Cc: fateman at EECS.Berkeley.EDU; MAXIMA LIST; Stavros Macrakis
> Subject: Re: [Maxima] fpprintprec
> 
> Barton Willis wrote:
> > By the way,  sometime ago I changed the equality testing in 
> the Maxima 
> > test suite. I found a bunch of tests
> > that failed with the new equality test because the last 
> digit of a float 
> > was missing in the expected answer.
> > (The old equality test was too forgiving.)
> > 
> >  So, it would be great if we had better printing of floats.
> 
> Floats or bfloats?  I'm not planning on doing anything about 
> floats.  If
> they're wrong, we need to complain about the underlying lisp.
> 
> I'm willing to fix it for bfloats, though.
> 
> Ray
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>