[Haskell-cafe] Re: [Haskell] How to get the binary respentation of?the Int/Double.

Chung-chieh Shan ccshan at post.harvard.edu
Wed Oct 29 15:32:35 EDT 2008


Ryan Ingram <ryani.spam at gmail.com> wrote in article <2f9b2d30810291058p2ea8c3d7g84bbfb311462f3e4 at mail.gmail.com> in gmane.comp.lang.haskell.cafe:
> Actually, this is a good question, at least as relating to floating
> point values.  Is there a primitive to view the machine representation
> of floats?

Not a primitive, but it can be defined:

module Cast (cast) where

import Foreign.Storable (Storable, sizeOf, peek)
import Foreign.Marshal.Utils (with)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr (castPtr)

cast :: (Storable a, Storable b) => a -> b
cast a = b where
  b | sizeOf a == sizeOf b = unsafePerformIO $ with a $ peek . castPtr

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
Don't mess with me lady; I've been drinking with skeletons.



More information about the Haskell-Cafe mailing list