flat-0.4.4: Principled and efficient bit-oriented binary serialization.

Safe HaskellTrustworthy
LanguageHaskell2010

Data.FloatCast

Description

Primitives to convert between Float/Double and Word32/Word64.

Code copied from binary.

Based on: http://hackage.haskell.org/package/reinterpret-cast-0.1.0/docs/src/Data-ReinterpretCast-Internal-ImplArray.html..

Implements casting via a 1-element STUArray, as described in http://stackoverflow.com/a/7002812/263061.

Synopsis

Documentation

floatToWord :: Float -> Word32 Source #

Reinterpret-casts a Float to a Word32.

wordToFloat :: Word32 -> Float Source #

Reinterpret-casts a Word32 to a Float.

\f -> wordToFloat (floatToWord f ) == f
>>> floatToWord (-0.15625)
3189768192
>>> wordToFloat 3189768192
-0.15625
>>> floatToWord (-5.828125) == 0xC0BA8000
True

doubleToWord :: Double -> Word64 Source #

Reinterpret-casts a Double to a Word64.

\f -> wordToDouble (doubleToWord f ) == f
>>> showHex (doubleToWord 1.0000000000000004) ""
"3ff0000000000002"
>>> doubleToWord 1.0000000000000004 == 0x3FF0000000000002
True
>>> showHex (doubleToWord (-0.15625)) ""
"bfc4000000000000"
>>> wordToDouble 0xbfc4000000000000
-0.15625

wordToDouble :: Word64 -> Double Source #

Reinterpret-casts a Word64 to a Double.

runST :: (forall s. ST s a) -> a #

Return the value computed by a state transformer computation. The forall ensures that the internal state used by the ST computation is inaccessible to the rest of the program.

cast :: (MArray (STUArray s) a (ST s), MArray (STUArray s) b (ST s)) => a -> ST s b Source #

>>> runST (cast (0xF0F1F2F3F4F5F6F7::Word64)) == (0xF0F1F2F3F4F5F6F7::Word64)
True