[Haskell-cafe] Data.Binary and serialising doubles in IEEE format

allan a.d.clark at ed.ac.uk
Wed Jan 9 08:18:52 EST 2008


Dear all

I have what I think should be a relatively simple problem:
I have a C program which reads in data from a binary file, it is a 
specific format so it knows that there are 3 integers followed by three 
doubles or whatever. So I'm trying to serialise the data from within a 
Haskell program and write it out to a file such that the C program can 
read this.
I've gotten the integers to work no problem but I'm having difficulty 
with the doubles.

My method is to use the Data.Binary library to write the data out as a 
bytestring and then simply write this out to disk.
The problem is that the instance of Binary for  Double does not 
serialise the data in the format which is expected by the C program 
(standard IEEE format using fread and a cast).

Now there was a thread on (essentially) this very same topic on 
haskell-cafe back in April, see:
http://www.haskell.org/pipermail/haskell-cafe/2007-April/024596.html

There was some discussion about the 'right' thing to do and that the 
library intended to be split into two separate portions; one for users 
that wish to serialise their own data and have it readable by the same 
haskell program, and those that wish to communicate with programs 
written in other languages.

 From the above thread:

> Perhaps we just don't care about ARM or other arches where GHC runs that
> do not use IEEE formats, I don't know. If that were the case we'd say
> something like:
> 
> instance Binary Double where
>   put d = assert (isIEEE (undefined :: Double)) $ do
>             write (poke d)

Now for my specific problem I'd be happy if I could write a function:
putDouble :: Double -> Put

and so obviously I tried:
putDouble :: Double -> Put
putDouble d = assert (isIEEE (undefined :: Double)) $ do
             write (poke d)

However I seem to be stuck at this point, where does the 'write' 
function come from?

Essentially then, is there anyone that can write my 'putDouble' function 
or give some hint as to how I might do it.
For the moment assume that I'm not really concerned with portability 
across platforms at least for the time being (I certainly don't think 
that the C program I'm attempting to communicate with is particularly 
portable anyway).

kind regards
allan



More information about the Haskell-Cafe mailing list