Re: [Haskell-cafe] Begginer question about alignment in Storable
If you are using hsc2hs (if you are using Cabal this is easy; just rename the file to *.hsc and Cabal will take care of the rest), then there is a macro for making this easier and so you don't have to think about it. First, place the following at the top of your source file: #let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__) Then #{alignment c_type} will always be expanded by hsc2hs to the correct alignment for the C type named c_type. On Sat, Nov 15, 2008 at 3:29 PM, Maurício <[EMAIL PROTECTED]> wrote: > Hi, > > I need to make the following structure into > an instance of Storable: > > struct { > double w[2]; > } > > Can I assume that its function 'alignment' should > return the same as "alignment (1::CDouble)"? Or > should it be "2 * alignment (1::CDouble)"? > > (I've read the wikipedia article about memory alignment, > and what I understand is that there's a better alignment > for hardware efficiency but compilers and programmers can > do almost anything about it. What can I do when writing > a library wrapper if I don't know what kind of compiler > pragmas were used in all platforms that library was > compiled?) > > Thanks, > Maurício > > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Begginer question about alignment in Storable
Hello Maurício, Saturday, November 15, 2008, 11:29:23 PM, you wrote: > struct { >double w[2]; > } > Can I assume that its function 'alignment' should > return the same as "alignment (1::CDouble)"? Or > should it be "2 * alignment (1::CDouble)"? secind - definitely not. first - probably yes -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Begginer question about alignment in Storable
Hi, I need to make the following structure into an instance of Storable: struct { double w[2]; } Can I assume that its function 'alignment' should return the same as "alignment (1::CDouble)"? Or should it be "2 * alignment (1::CDouble)"? (I've read the wikipedia article about memory alignment, and what I understand is that there's a better alignment for hardware efficiency but compilers and programmers can do almost anything about it. What can I do when writing a library wrapper if I don't know what kind of compiler pragmas were used in all platforms that library was compiled?) Thanks, Maurício ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Begginer question
Maurício <[EMAIL PROTECTED]> writes: > complex_root :: (Float, Float, Float) -> (Complex Float, Complex Float) > complex_root (a,b,c) = (x1,x2) where { > delta = b * b - 4 * a * c :: Float; > sqr_delta = if delta >= 0 then (sqrt delta) :+ 0 else 0 :+ > (sqrt delta) :: (Complex Float); > > x1 = (b + sqr_delta)/(2 * a); > x2 = (b - sqr_delta)/(2 * a); > } > Couldn't match `Float' against `Complex Float' > Expected type: Float > Inferred type: Complex Float > In the second argument of `(+)', namely `sqr_delta' > In the definition of `x1': x1 = (b + sqr_delta) The error message says it all, really. If you examine the definition of x1, you see that (+) is applied to two variables. If you check these, you will discover that they have different types, which is the cause of the error. Note that Haskell doesn't automatically convert arguments for you -- this is a feature. -kzm -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Begginer question
On 6 Jan 2005, at 01:37, Maurício wrote: import Complex; complex_root :: (Float, Float, Float) -> (Complex Float, Complex Float) complex_root (a,b,c) = (x1,x2) where { delta = b * b - 4 * a * c :: Float; sqr_delta = if delta >= 0 then (sqrt delta) :+ 0 else 0 :+ (sqrt delta) :: (Complex Float); x1 = (b + sqr_delta)/(2 * a); x2 = (b - sqr_delta)/(2 * a); } Couldn't match `Float' against `Complex Float' Expected type: Float Inferred type: Complex Float In the second argument of `(+)', namely `sqr_delta' In the definition of `x1': x1 = (b + sqr_delta) Can you help me finding what is wrong? Shouldn't "b" be converted to Complex Float and be summed to sqr_delta? Haskell will not automatically convert b from Float to Complex Float. The arguments of (+) should have the same type. One alternative is to use b :+ 0 instead of b. (and similarly for a). Another approach is to define a 'cast' function like: toComplex x = (fromRational.toRational) x :: Complex Float and then you can use toComplex b instead of b :+ 0. that's more characters to type, though... Note that sqr_delta isn't going to be defined as you expect, either. Since delta has type Float, sqrt delta has type Float. (And sqrt -1 :: Float is Not A Number). If you want to do it by hand this way, then you want: sqr_delta = if delta >= 0 then (sqrt delta) :+ 0 else 0 :+ (sqrt (-delta)) :: (Complex Float); If delta was itself already Complex, then sqrt would do the right thing automatically. Jules ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Begginer question
Hi, When compiling this: import Complex; complex_root :: (Float, Float, Float) -> (Complex Float, Complex Float) complex_root (a,b,c) = (x1,x2) where { delta = b * b - 4 * a * c :: Float; sqr_delta = if delta >= 0 then (sqrt delta) :+ 0 else 0 :+ (sqrt delta) :: (Complex Float); x1 = (b + sqr_delta)/(2 * a); x2 = (b - sqr_delta)/(2 * a); } *** I get this error message: ** Couldn't match `Float' against `Complex Float' Expected type: Float Inferred type: Complex Float In the second argument of `(+)', namely `sqr_delta' In the definition of `x1': x1 = (b + sqr_delta) *** Can you help me finding what is wrong? Shouldn't "b" be converted to Complex Float and be summed to sqr_delta? Thanks for your help, Maurício ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe