Hello, Is it possible in Haskell to access the underlying machine bit representation of a Float or Double value? I need to be able to be able to send this bit representation as a list of bytes in network byte order to a process running on a different platform (with a different host byte order to my platform). For reference, I run Haskell under Linux on Intel. The processes I want to communicate with run under Sun Solaris, Hitachi HPUX and Java everywhere. Any suggestions, for any Haskell translator much appreciated. Thanks. _____________________________________________________________ Global Virtual Desktop http://www.magicaldesk.com
pachinko@magicaldesk.com wrote:
Hello,
Is it possible in Haskell to access the underlying machine bit representation of a Float or Double value?
I need to be able to be able to send this bit representation as a list of bytes in network byte order to a process running on a different platform (with a different host byte order to my platform).
For reference, I run Haskell under Linux on Intel. The processes I want to communicate with run under Sun Solaris, Hitachi HPUX and Java everywhere.
Any suggestions, for any Haskell translator much appreciated.
Thanks.
Look at encodeFloat and decodeFloat, documented in section 6.4.6 of The Haskell 98 Report. They offer "efficient, machine-independent access to the components of a floating-point number". See http://www.haskell.org/onlinereport/basic.html#sect6.4.6 Matt Harden
On 2001-07-24T16:51:54-0700, pachinko@magicaldesk.com wrote:
Hello,
Is it possible in Haskell to access the underlying machine bit representation of a Float or Double value?
I need to be able to be able to send this bit representation as a list of bytes in network byte order to a process running on a different platform (with a different host byte order to my platform).
For reference, I run Haskell under Linux on Intel. The processes I want to communicate with run under Sun Solaris, Hitachi HPUX and Java everywhere.
Any suggestions, for any Haskell translator much appreciated.
If you're using GHC, you should be able to get at the raw bits of the machine representation using some Storable + Ptr + MarshallAlloc trickery. (These are modules in -package lang.) I tested the following on GHC5 on i386-linux: module Cast (cast) where import Storable (Storable, sizeOf, peek) import MarshalUtils (withObject) import IOExts (unsafePerformIO) import Ptr (castPtr) cast :: (Storable a, Storable b) => a -> b cast a = b where b | sizeOf a == sizeOf b = unsafePerformIO $ withObject a $ peek . castPtr I was able to get (cast :: Int -> Char) 98 == 'b' This might work with other compilers as well, but I don't know... Fun fun fun! More efficient implementations? -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig « ne gâche pas ta vie pour leur idée de patrie » le bouton me dit
participants (3)
-
Ken Shan -
Matt Harden -
pachinko@magicaldesk.com