On 27-Jun-09, at 12:17 AM, Marc Feeley wrote:
The code below will write and read 64 bit floats. The IEEE 754 representation is not guaranteed, but on most machines that is what you'll get. Beware that the endianness will be determined by the processor's native endianness.
I forgot to mention these conversion procedures which specifically implement the IEEE 754 encoding of 32 and 64 bit floating point numbers. They convert inexact reals to and from an exact integer which is its 32 or 64 bit representation.
(##flonum.->ieee754-32 x) ;; convert flonum x to 32 bit representation (##flonum.->ieee754-64 x) ;; convert flonum x to 64 bit representation (##flonum.<-ieee754-32 n) ;; convert 32 bit representation n to flonum (##flonum.<-ieee754-64 n) ;; convert 64 bit representation n to flonum
Here are a few examples:
(##flonum.->ieee754-64 3.14159)
4614256650576692846
(##flonum.<-ieee754-64 4614256650576692846)
3.14159
(number->string (##flonum.->ieee754-32 1.0) 16)
"3f800000"
(number->string (##flonum.->ieee754-64 1.0) 16)
"3ff0000000000000"
(number->string (##flonum.->ieee754-64 -1.0) 16)
"bff0000000000000"
(number->string (##flonum.->ieee754-64 2.0) 16)
"4000000000000000"
(number->string (##flonum.->ieee754-64 (+ 1.0 (expt 0.5 52))) 16)
"3ff0000000000001"
(number->string (##flonum.->ieee754-64 +inf.0) 16)
"7ff0000000000000"
Marc