2008/11/11/00.04.35

Lisp function for converting a sequence of bytes (64 bits) to IEEE-754 Double

Brought to you, not just in part, but almost in entirety through documentation from Sun on IEEE-754.

This code is a bit messy, so I’ll probably come back in a few days with a post showing a cleaner version.

; This doesn't recognize -INF or +INF
; It wouldn't be hard to add, I just figure
;    I won't see them in what I'm doing.
(defun seq-to-IEEE-754-double (seq)
  (let* ((int (seq-to-int seq))
         (s (ash int -63))
         (e (ash (logxor
                   (ash (ash int -63) 63)
                   (ash (ash int -52) 52))
                 -52))
         (f (logxor
              (ash (ash int -52) 52)
              int)))
    ;(format t "~&~64,'0b~%~b ~11,'0b ~52,'0b~%" int s e f)
    ;(format t "~&~f ~f ~f ~f~%" s e f (/ f (expt 10 (digits f))))

    (if (and (< 0 e) (< e 2047))
      (* (expt -1 s)
         (expt 2 (- e 1023))
         (+ 1 (/ f (expt 10 (digits f)))))

      (if (and (= e 0) (not (= f 0)))
        (* (expt -1 s)
           (expt 2 -1022)
           (/ f (expt 10 (digits f))))

        (if (and (= e 0) (= f 0))
          (* (expt -1 s)
             0.0)
             ; Yes, something times zero. It's a signed zero.
             ; Don't know if it matters, but it's what the docs said to do.

          'NaN)))))}}}

Original post by richard.lyman and software by Elliott Back

Comments are closed.