Subject: [clisp-list] writing FP numbers

From: Robert Brown <[email-masked]>
Date: Tue May 25 11:17:08 2004

I would like to write a value of type single-float into an array of 8-bit
bytes using the binary format that C code would use.  Something similar to:

  float foobar;
  foobar = 3.14159;
  memcpy(buffer, &foobar, 4);

I can decode a single-float with integer-decode-float in order to get the
correct bits, but things get a little bit tricky when the float is
denormalized.  Also, I'm unsure how integer-decode-float handles NaNs.

Is there a non-ANSI internal clisp function that does what I want quickly
and reliably?  For example, SBCL has sb-kernel:single-float-bits, which
returns an integer containing the bit representation of its floating point
argument.

Also, given the raw bits for a floating point number, what clisp function
can be used to construct a single-float or double-float?

            bob

---

From: Sam Steingold <[email-masked]>
Date: Tue May 25 13:46:05 2004

> * Robert Brown <[email-masked]> [2004-05-25 11:16:45 -0700]:
>
> I can decode a single-float with integer-decode-float in order to get
> the correct bits, but things get a little bit tricky when the float is

CLISP does not support denormalized floats or NaNs

> Is there a non-ANSI internal clisp function that does what I want
> quickly and reliably?
>
> Also, given the raw bits for a floating point number, what clisp
> function can be used to construct a single-float or double-float?

what you want are READ-FLOAT and WRITE-FLOAT
<http://clisp.cons.org/impnotes/stream-dict.html#bin-input>
<http://clisp.cons.org/impnotes/stream-dict.html#bin-output>

what you are asking for is implemented in the form of FLOAT->LIST and
LIST->FLOAT in
<http://cvs.sourceforge.net/viewcvs.py/clisp/clisp/tests/bin-io.tst>

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
I may be getting older, but I refuse to grow up!

---

From: Robert Brown <[email-masked]>
Date: Tue May 25 14:21:19 2004

Thanks very much!  I'll take a look at the documentation and the test code.

        bob

====================