flat-0.4.4.0.0.0.0.2: Principled and efficient bit-oriented binary serialization.
Safe Haskell Trustworthy
Language Haskell2010

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

wordToFloat :: Word32 -> Float Source #

Reinterpret-casts a Word32 to a Float .

\f -> wordToFloat (floatToWord f ) == f

+++ OK, passed 100 tests.

>>> 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

+++ OK, passed 100 tests.

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

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

Return the value computed by a state thread. 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