Reading 32-bit IEEE floats from binary file ??



Here's what I have so far.  This code doesn't handle -0.0s0.

I don't know if it works correctly on gradually underflowing numbers.

I don't know how to get Lisp to deal correctly with NaN's.

It seems to work on most other numbers.

(defun interpret-real32 (n)
  ;;; Interpret 32-bit integer n as 32-bit float.
  (let* ((signif (logior (logand n #.(1- (ash 1 23))) #.(ash 1 23)))
         (expon (logand 255 (ash n -23)))
         (signif (if (zerop expon) 0 signif))
         (expon (- expon 150))
         (sign (- 1 (ash (ash n -31) 1))))
    (scale-float (* (float sign 1.0s0)
                    (float signif 1.0s0))
                 expon)))

At 12:47 AM 5/24/2013, Robert Dodier wrote:
>On 2013-05-24, Henry Baker <hbaker1 at pipeline.com> wrote:
>
>> Is there a Common Lisp package that knows how to read binary format
>> 32-bit IEEE floats from a binary file?
>
>There is code in share/numericalio to read and write floats. There is
>code for 32-bit floats although it is not very accessible from Maxima
>since Maxima prefers 64-bit floats. Of course, one can call the 32-bit
>code from Lisp.
>
>Note that there's no guarantee that 32-bit and 64-bit IEEE-754 floats
>correspond exactly to Lisp float types. The numericalio code guesses
>that double and long floats are 64 bits and short and single are 32
>bits. This is incorrect for at least one known Lisp -- GCL, in which all
>floats are 64 bits iirc.
>
>See the documentation for read_binary_data, write_binary_data, and
>assume_external_byte_order.
>
>best
>
>Robert Dodier