Re: [Haskell-cafe] Re: Just for a laugh...
Jon Fairbairn wrote: "David House" <[EMAIL PROTECTED]> writes: On 31/05/07, Andrew Coppin <[EMAIL PROTECTED]> wrote: If you're bored... can you come up with a solution to this? Try using floatToDigits: http://haskell.org/ghc/docs/latest/html/libraries/base/Numeric.html#v%3AfloatToDigits "floatToDigits takes a base and a non-negative RealFloat number, and returns a list of digits and an exponent." I think you also need floatRadix and floatDigits. Note that the challenge asks for the "internal" bitmap representation of an IEEE double-precision integer - not the mathematical binary expansion. (In particular, things like Infinity and NaN have special bit sequences.) Did I mention that this is a silly challange yet? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Just for a laugh...
On 31/05/07, Andrew Coppin <[EMAIL PROTECTED]> wrote: Note that the challenge asks for the "internal" bitmap representation of an IEEE double-precision integer - not the mathematical binary expansion. (In particular, things like Infinity and NaN have special bit sequences.) Ah, sorry, then disregard my solution. I did wonder why you'd immediately jump to Data.Bits. -- -David House, [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Just for a laugh...
On 31 May 2007 21:52:33 +0100, Jon Fairbairn <[EMAIL PROTECTED]> wrote: Yes, but you didn't say that it's not only silly but demonstrates the opposite of expressiveness as it's all about breaking an abstraction and must be non-portable code (because it's definition is that it won't give the same results on different hardware), so such code should be *hard* to write in a good language. Well, I would suggest that maybe *good* is not completely congruent with *expressive* (at least for this case). If I want to write a program to learn how IEEE floats are constructed, by destructing them, then I *should* be able to. I have no solutions of my own though :-( I wait in eager expectation D. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Just for a laugh...
Andrew Coppin wrote: > Note that the challenge asks for the "internal" bitmap representation of > an IEEE double-precision integer Actually it didn't. It asked for the machine's internal representation of a double precision float, and you are not guaranteed that this representation conforms to IEEE 7-whatsit. It is beyond me what you're going to do with that unspecified representation, though. > Did I mention that this is a silly challange yet? Silly, yes. Challenge, no. We can do the same the C++ guy did, it's only a question if we need to. Something close to this: | import Foreign.Marshal | import Foreign.Ptr | import Data.Word | | valueToBytes :: Storable a => a -> IO [Word8] | valueToBytes a = with a $ \p -> peekBytes (castPtr b) (sizeOf a) Reversing the list and hammering it down to single bits is trivial after that. -Udo -- Structure is _nothing_ if it is all you got. Skeletons _spook_ people if they try to walk around on their own. I really wonder why XML does not. -- Erik Naggum signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Just for a laugh...
The site seems to be asking for the internal floating point representation. So it doesn't matter if it's IEEE 754, if the ints are 2-complements, or whatever. I used this code as a quick hack for one of my programs, but I think it would work in this case. It should work for any Storable type. import qualified Data.ByteString as BS import Data.ByteString.Base import Foreign.ForeignPtr import Foreign.Storable binPut num = do fptr <- mallocForeignPtrBytes (sizeOf num) withForeignPtr (castForeignPtr fptr) (\x -> poke x num) BS.writeFile "/tmp/foo" (BS.reverse $ fromForeignPtr fptr (sizeOf num)) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Just for a laugh...
On 6/3/07, Rafael Almeida <[EMAIL PROTECTED]> wrote: The site seems to be asking for the internal floating point representation. So it doesn't matter if it's IEEE 754, if the ints are 2-complements, or whatever. I used this code as a quick hack for one of my programs, but I think it would work in this case. It should work for any Storable type. import qualified Data.ByteString as BS import Data.ByteString.Base import Foreign.ForeignPtr import Foreign.Storable binPut num = do fptr <- mallocForeignPtrBytes (sizeOf num) withForeignPtr (castForeignPtr fptr) (\x -> poke x num) BS.writeFile "/tmp/foo" (BS.reverse $ fromForeignPtr fptr (sizeOf num)) Ops, that reverse was needed for what I was doing, but not needed for this particular problem, so the code should actually be: import qualified Data.ByteString as BS import Data.ByteString.Base import Foreign.ForeignPtr import Foreign.Storable binPut num = do fptr <- mallocForeignPtrBytes (sizeOf num) withForeignPtr (castForeignPtr fptr) (\x -> poke x num) BS.writeFile "/tmp/foo" (fromForeignPtr fptr (sizeOf num)) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Just for a laugh...
almeidaraf: > On 6/3/07, Rafael Almeida <[EMAIL PROTECTED]> wrote: > >The site seems to be asking for the internal floating point > >representation. So it doesn't matter if it's IEEE 754, if the ints are > >2-complements, or whatever. I used this code as a quick hack for one of > >my programs, but I think it would work in this case. It should work for > >any Storable type. > > > >import qualified Data.ByteString as BS > >import Data.ByteString.Base > >import Foreign.ForeignPtr > >import Foreign.Storable > >binPut num = > >do > >fptr <- mallocForeignPtrBytes (sizeOf num) > >withForeignPtr (castForeignPtr fptr) (\x -> poke x num) > >BS.writeFile "/tmp/foo" (BS.reverse $ fromForeignPtr fptr (sizeOf > >num)) > > > Ops, that reverse was needed for what I was doing, but not needed for > this particular problem, so the code should actually be: > > import qualified Data.ByteString as BS > import Data.ByteString.Base > import Foreign.ForeignPtr > import Foreign.Storable > binPut num = >do >fptr <- mallocForeignPtrBytes (sizeOf num) >withForeignPtr (castForeignPtr fptr) (\x -> poke x num) >BS.writeFile "/tmp/foo" (fromForeignPtr fptr (sizeOf num)) ^^^ Interesting use of ByteStrings to print foreigin ptr arrays there. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe