float to bfloat



Here is a function that uses integer-docode-float to do float to
big float conversion.

Notes:

(1)  The function floattofp prints an warning when converting a float to 
a big float; this  function doesn't -- should it?

(2)  For denormalized inputs, the function floattofp halts -- try 
 bfloat(2.0d0^-1025). (It seems that gcl 2-5.0 doesn't support
denormalized floats?  Under cmucl,  bfloat(2.0d0^-1025) gives an 
error.) This function should work correctly on denormalized floats. 

(3)  Incidently, the big float to float conversion function has a bug -- 
try float(float2bfloat(2.0d0^1023)). 

(4)  The function bigfloatp converts a big float to a big float with
the current value of fpprec.

(5) I don't know what $float2bfloat does when the input is  NaN.

(6) I haven't tested this function for single or long floats.  Have we
expunged single floats from Maxima?

;; If the input is a single or double float, convert the input to
;; the nearest big float; otherwise, return the input.

(defun $float2bfloat (x)
  (cond ((floatp x)
         (bigfloatp 
          (multiple-value-bind (fp n s) (integer-decode-float x)
            (if (= s -1) (setq fp (- fp)))
            (setq s (float-digits x))
            `((bigfloat simp ,s) ,fp ,(+ s n)))))
        (t x)))

Some simple tests

(C1) load("l:/float2bf.lisp");
(D1)                                   l:/float2bf.lisp
(C2) float2bfloat(0.0d0);
(D2)                                        0.0B0
(C3) float2bfloat(-0.0d0);
(D3)                                        0.0B0
(C4) float2bfloat(1.0d0);
(D4)                                        1.0B0
(C5) float2bfloat(2.0d0^-1023);
(D5)                                1.668805393880401B-308
(C6) fpprec : 35;
(D6)                                          35
(C7) float2bfloat(2.0d0^-1023);
(D7)                       1.668805393880401037317674537999303B-308
(C8) float2bfloat(2.0d0^1023);
(D8)                       8.9884656743115795386465259539451237B307
(C9) x : float(8/9)$
(C10) float2bfloat(x);
(D10)                      8.8888888888888883954564334999304265B-1
(C11) float(%);
(D11)                                 0.8888888888888888


(C13) build_info();

Maxima version: 5.9.0
Maxima build date: 19:10 2/9/2003
host type: i686-pc-mingw32
lisp-implementation-type: Kyoto Common Lisp
lisp-implementation-version: GCL-2-5.0

Barton