Fortran mixed mode arithmetic expressions -> Haskell

Translating Fortran mixed mode arithmetic expressions into Haskell is quite a challenge. Believe it or not c=10.**(11-int(alog10(r)+10)) translates to let c = (**) 10.0 $ fromIntegral $ subtract 11 $ truncate $ (+) (logBase 10.0 r) 10.0 I finally broke the expression below into two parts (k1 & k2) to ease translation. I get it that Haskell is expecting to subtract two Integers and is instead being given an Integer and a Double. What must I do to make this work? Are there any guidelines for doing this kind of translation work? Michael ================ Prelude> let mm = 2 Prelude> let k1 = 3*mm+2 Prelude> let k2 = (/) 150 119 Prelude> let k = k1 - k2 <interactive>:1:13: Couldn't match expected type `Integer' against inferred type `Double' In the second argument of `(-)', namely `k2' In the expression: k1 - k2 In the definition of `k': k = k1 - k2 Prelude> :t 150/119 150/119 :: (Fractional t) => t Prelude>

Am Montag 26 Oktober 2009 04:21:06 schrieb michael rice:
Translating Fortran mixed mode arithmetic expressions into Haskell is quite a challenge. Believe it or not
c=10.**(11-int(alog10(r)+10))
translates to
let c = (**) 10.0 $ fromIntegral $ subtract 11 $ truncate $ (+) (logBase 10.0 r) 10.0
No, subtract 11 x is x-11, not 11-x. let c = 10^^(11 - (truncate (logBase 10 r) + 10)) or, to make it a little simpler, let c = 10^^(1 - truncate (logBase 10 r)) Prelude> :t (^^) (^^) :: (Fractional a, Integral b) => a -> b -> a That is probably faster and more accurate than (**).
I finally broke the expression below into two parts (k1 & k2) to ease translation. I get it that Haskell is expecting to subtract two Integers and is instead being given an Integer and a Double. What must I do to make this work? Are there any guidelines for doing this kind of translation work?
Michael
================
Prelude> let mm = 2 Prelude> let k1 = 3*mm+2 Prelude> let k2 = (/) 150 119 Prelude> let k = k1 - k2
<interactive>:1:13: Couldn't match expected type `Integer' against inferred type `Double' In the second argument of `(-)', namely `k2' In the expression: k1 - k2 In the definition of `k': k = k1 - k2 Prelude> :t 150/119 150/119 :: (Fractional t) => t Prelude>
In this case, :set -XNoMonoMorphismRestriction does the trick. In general, you have to use fromInteg[er/ral] and other conversion functions explicitly. let k = fromIntegral k1 - k2

Hi Daniel,
I guess I only accidentally got the right answer for c=....
For my r value of 60.0
*Main> truncate $ (+) (logBase 10.0 60.0) 10.0
11
*Main>
and subtract 11 11 == 0
Thanks for correcting me.
Also missed that 11-10=1 simplification. Forest for the trees stuff.
Your translation's definitely an improvement.
I looked for an exponential operator and grabbed the first one I
found. In the Prelude (**) is under the heading Methods, while (^^)
is under the heading Numeric Functions. Reasoning?
There's a lot of "automatic" stuff going on in Fortan mixed mode expressions, like variables that begin with I-N being integers by default, and the rest being real by default, so it's probably safer to keep things explicit in the Haskell translations.
Thanks again.
Michael
--- On Sun, 10/25/09, Daniel Fischer
Translating Fortran mixed mode arithmetic expressions into Haskell is quite a challenge. Believe it or not
c=10.**(11-int(alog10(r)+10))
translates to
let c = (**) 10.0 $ fromIntegral $ subtract 11 $ truncate $ (+) (logBase 10.0 r) 10.0
No, subtract 11 x is x-11, not 11-x. let c = 10^^(11 - (truncate (logBase 10 r) + 10)) or, to make it a little simpler, let c = 10^^(1 - truncate (logBase 10 r)) Prelude> :t (^^) (^^) :: (Fractional a, Integral b) => a -> b -> a That is probably faster and more accurate than (**).
I finally broke the expression below into two parts (k1 & k2) to ease translation. I get it that Haskell is expecting to subtract two Integers and is instead being given an Integer and a Double. What must I do to make this work? Are there any guidelines for doing this kind of translation work?
Michael
================
Prelude> let mm = 2 Prelude> let k1 = 3*mm+2 Prelude> let k2 = (/) 150 119 Prelude> let k = k1 - k2
<interactive>:1:13: Couldn't match expected type `Integer' against inferred type `Double' In the second argument of `(-)', namely `k2' In the expression: k1 - k2 In the definition of `k': k = k1 - k2 Prelude> :t 150/119 150/119 :: (Fractional t) => t Prelude>
In this case, :set -XNoMonoMorphismRestriction does the trick. In general, you have to use fromInteg[er/ral] and other conversion functions explicitly. let k = fromIntegral k1 - k2

On Oct 26, 2009, at 01:00 , michael rice wrote:
I looked for an exponential operator and grabbed the first one I found. In the Prelude (**) is under the heading Methods, while (^^) is under the heading Numeric Functions. Reasoning?
It's correct if perhaps not ideal for someone who doesn't think in terms of Haskell. (**) is a member of a typeclass, whereas (^^) is an independent function; you are expected to check that the typeclass is appropriate for what you're doing. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Hi Brandon,
Being new to Haskell, I take it (^) and (^^) would be the preferred exponential "operator." When (how,where,why) would one use (**)?
Michael
--- On Mon, 10/26/09, Brandon S. Allbery KF8NH

Hello michael, Monday, October 26, 2009, 7:24:46 PM, you wrote: afair, ** and ^ are different - one is for integers, another for floating-point numbers
Hi Brandon,
Being new to Haskell, I take it (^) and (^^) would be the preferred exponential "operator." When (how,where,why) would one use (**)?
Michael
--- On Mon, 10/26/09, Brandon S. Allbery KF8NH
wrote:
From: Brandon S. Allbery KF8NH
Subject: Re: [Haskell-cafe] Fortran mixed mode arithmetic expressions -> Haskell To: "michael rice" Cc: "Brandon S. Allbery KF8NH" , haskell-cafe@haskell.org, "Daniel Fischer" Date: Monday, October 26, 2009, 12:16 PM
On Oct 26, 2009, at 01:00 , michael rice wrote: I looked for an exponential operator and grabbed the first one I found. In the Prelude (**) is under the heading Methods, while (^^) is under the heading Numeric Functions. Reasoning?
It's correct if perhaps not ideal for someone who doesn't think in terms of Haskell. (**) is a member of a typeclass, whereas (^^) is an independent function; you are expected to check that the typeclass is appropriate for what you're doing.
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Am Monday 26 October 2009 17:24:46 schrieben Sie:
Being new to Haskell, I take it (^) and (^^) would be the preferred exponential "operator." When (how,where,why) would one use (**)?
The beasts have different types and are for different things: Prelude> :i (^) (^) :: (Num a, Integral b) => a -> b -> a -- Defined in GHC.Real infixr 8 ^ This one raises any number to a nonnegative integral power. A typical implementation would be power by repeated squaring. Prelude> :i (^^) (^^) :: (Fractional a, Integral b) => a -> b -> a -- Defined in GHC.Real infixr 8 ^^ This one allows also negative powers, so the type of base must allow inversion, hence it must belong to Fractional. The exponent must still be an integer, you can't use this for n-th roots or similar. A typical implementation would be power by repeated squaring, followed by (1/) if the exponent is negative. Prelude> :i (**) class (Fractional a) => Floating a where ... (**) :: a -> a -> a ... -- Defined in GHC.Float infixr 8 ** This one raises a floating point number to an arbitrary power, so you can use it for n-th roots. A typical implementation would be b ** e = exp (e*log b).

Got it. No doubt some of this figures into why I was beaten bloody by ghci last night. Is there a number "tree" somewhere that shows the heirarchy?
Michael
--- On Mon, 10/26/09, Daniel Fischer
Being new to Haskell, I take it (^) and (^^) would be the preferred exponential "operator." When (how,where,why) would one use (**)?
The beasts have different types and are for different things: Prelude> :i (^) (^) :: (Num a, Integral b) => a -> b -> a -- Defined in GHC.Real infixr 8 ^ This one raises any number to a nonnegative integral power. A typical implementation would be power by repeated squaring. Prelude> :i (^^) (^^) :: (Fractional a, Integral b) => a -> b -> a -- Defined in GHC.Real infixr 8 ^^ This one allows also negative powers, so the type of base must allow inversion, hence it must belong to Fractional. The exponent must still be an integer, you can't use this for n-th roots or similar. A typical implementation would be power by repeated squaring, followed by (1/) if the exponent is negative. Prelude> :i (**) class (Fractional a) => Floating a where ... (**) :: a -> a -> a ... -- Defined in GHC.Float infixr 8 ** This one raises a floating point number to an arbitrary power, so you can use it for n-th roots. A typical implementation would be b ** e = exp (e*log b).

http://www.haskell.org/onlinereport/basic.html 6.3 Standard Haskell Classes -Ross On Oct 26, 2009, at 3:13 PM, michael rice wrote:
Got it. No doubt some of this figures into why I was beaten bloody by ghci last night. Is there a number "tree" somewhere that shows the heirarchy?
Michael
--- On Mon, 10/26/09, Daniel Fischer
wrote: From: Daniel Fischer
Subject: Re: [Haskell-cafe] Fortran mixed mode arithmetic expressions -> Haskell To: "michael rice" , haskell-cafe@haskell.org Date: Monday, October 26, 2009, 1:09 PM Am Monday 26 October 2009 17:24:46 schrieben Sie:
Being new to Haskell, I take it (^) and (^^) would be the preferred exponential "operator." When (how,where,why) would one use (**)?
The beasts have different types and are for different things: Prelude> :i (^) (^) :: (Num a, Integral b) => a -> b -> a -- Defined in GHC.Real infixr 8 ^
This one raises any number to a nonnegative integral power. A typical implementation would be power by repeated squaring.
Prelude> :i (^^) (^^) :: (Fractional a, Integral b) => a -> b -> a -- Defined in GHC.Real infixr 8 ^^
This one allows also negative powers, so the type of base must allow inversion, hence it must belong to Fractional. The exponent must still be an integer, you can't use this for n-th roots or similar. A typical implementation would be power by repeated squaring, followed by (1/) if the exponent is negative.
Prelude> :i (**) class (Fractional a) => Floating a where ... (**) :: a -> a -> a ... -- Defined in GHC.Float infixr 8 **
This one raises a floating point number to an arbitrary power, so you can use it for n-th roots. A typical implementation would be b ** e = exp (e*log b).
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Oct 26, 2009 at 20:13, michael rice wrote:
Got it. No doubt some of this figures into why I was beaten bloody by ghci last night. Is there a number "tree" somewhere that shows the heirarchy?
http://www-bucephalus-org.blogspot.com/2009/09/haskell-number-system-in-one-... Sean
participants (6)
-
Brandon S. Allbery KF8NH
-
Bulat Ziganshin
-
Daniel Fischer
-
michael rice
-
Ross Mellgren
-
Sean Leather