Antw.: Question about CFloat and Float

Hi Alberto,
thanks for the hint and the link. Do you have any information on whether that will continue to work in the future? I somehow have a bad feeling about it because the C* versions of the types would then be unnecessary.
Thanks again,
Christian
----- Reply message -----
Von: "Alberto Ruiz"
Dear list members, I have the following problem: I have wrapped a C library using the FFI and c2hs. The C functions all take a bunch of pointers to C arrays of floats, doubles, or ints as arguments. Naturally, the Haskell representation of these is e.g. Ptr CFloat or, for single values, CFloat. My question is: if I have an array, e.g. allocated with mallocArray, to Haskell Float values (Ptr Float), is the representation of Float exactly the same as for CFloat? Can I safely use Ptr Float where the C function expects the C type float* ? I assume that I can not, and in that case I would like to find a way to have Float on the Haskell side and CFloat on the C side. Any hint on how I would do that without copying data and without too large performance penalties?
Thanks a lot, Christian
I think that in ghc you can assume the same representation for CFloat and CDouble, they are newtypes of Float and Double: http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Foreign-C-... (I directly use arrays of Float and Double with C libraries without any problem). But Int may be different from CInt depending on the architecture, so we need explicit conversions. Alberto

On 04/29/2011 08:20 AM, ch.gosch@googlemail.com wrote:
Hi Alberto, thanks for the hint and the link. Do you have any information on whether that will continue to work in the future? I somehow have a bad feeling about it because the C* versions of the types would then be unnecessary.
I don't have this information, but I find it very convenient that Float and Double are the standard IEEE 754 floating types. I hope this is not changed any time soon... :) http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_mode... Alberto
Thanks again, Christian
----- Reply message ----- Von: "Alberto Ruiz"
An: "C Gosch" Cc: Betreff: [Haskell-beginners] Question about CFloat and Float Datum: Do., Apr. 28, 2011 15:27 Hi Christian,
On 04/28/2011 02:27 PM, C Gosch wrote:
Dear list members, I have the following problem: I have wrapped a C library using the FFI and c2hs. The C functions all take a bunch of pointers to C arrays of floats, doubles, or ints as arguments. Naturally, the Haskell representation of these is e.g. Ptr CFloat or, for single values, CFloat. My question is: if I have an array, e.g. allocated with mallocArray, to Haskell Float values (Ptr Float), is the representation of Float exactly the same as for CFloat? Can I safely use Ptr Float where the C function expects the C type float* ? I assume that I can not, and in that case I would like to find a way to have Float on the Haskell side and CFloat on the C side. Any hint on how I would do that without copying data and without too large performance penalties?
Thanks a lot, Christian
I think that in ghc you can assume the same representation for CFloat and CDouble, they are newtypes of Float and Double:
http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Foreign-C-...
(I directly use arrays of Float and Double with C libraries without any problem). But Int may be different from CInt depending on the architecture, so we need explicit conversions.
Alberto
participants (2)
-
Alberto Ruiz
-
ch.gosch@googlemail.com