[Haskell-cafe] CDouble type coercion
Sven Panne
Sven.Panne at aedion.de
Sun May 14 09:59:58 EDT 2006
Am Sonntag, 14. Mai 2006 09:30 schrieb SevenThunders:
> I am new to Haskell and found myself in a bind concerning the use of
> the C types, CDouble in particular. I extract a CDouble via it's pointer
> from a StorableArray. Since the array must interface with C the elements
> of the array must be CDouble. Now I'd like to use Text.Printf to format
> print statements of elements of the array, but Text.Printf requires Doubles
> as inputs and so far I have not found an obvious way to coerce CDoubles
> into Doubles. [...]
You can use the Prelude function realToFrac to convert between the various
floating-point types:
----------------------------------------------------------------
panne at jeanluc:~> ghci -v0
Prelude> :t realToFrac
realToFrac :: (Fractional t1, Real t) => t -> t1
Prelude> (realToFrac :: Foreign.C.Types.CDouble -> Double) 1234.5
1234.5
----------------------------------------------------------------
As you can see from its type, realToFrac is not exactly about floating-point
conversions, but for almost all practical use cases it is. :-)
Cheers,
S.
More information about the Haskell-Cafe
mailing list