jeremy at n-heptane.com
Sun Sep 14 20:54:52 EDT 2008
If we look at these two examples, it appears that the results are reversed:
Prelude> let n o = (-1 `o` 1) in n (-)
Prelude> let n o = (-1 `o` 1) in n (+)
we expect (-1 - 1) = -2 and (-1 + 1) = 0, but we get the opposite.
Due to operator precedence, the equations are being interpreted as:
Prelude> let n o = (-(1 `o` 1)) in n (-)
Prelude> let n o = (-(1 `o` 1)) in n (+)
The fix is to use parens around a negative term:
Prelude> let n o = ((-1) `o` 1) in n (-)
Prelude> let n o = ((-1) `o` 1) in n (+)
I have heard complaints about the negation sign in Haskell before, I
suppose this is why. Hopefully someone else can provide more details.
ps. You could probably also fix it by changing the precedence of the
`o` operator. I believe that is possible. But parens around (-b) is
the more standard solution.
At Mon, 15 Sep 2008 02:24:14 +0200,
Cetin Sert wrote:
> Hi why do I get?
> cetin at linux-d312:~/lab/exp/1> ./eq
> a = b = c = n1-0.8457820374040622n2-0.1542179625959377
> when I run
> import System.IO
> main :: IO ()
> main = do
> a ← ask "a"
> b ← ask "b"
> c ← ask "c"
> eval a b c
> ask v = do
> putStr (v ++ " = ")
> eval a b c = do
> case delta < 0 of
> True → putStr "neg"
> False → putStr ("n1" ++ show n1 ++ "n2" ++ show n2)
> delta = b*b - 4*c*a
> n o = (-b `o` sqrt(delta))/(2*a)
> n1 = n (+)
> n2 = n (-)
More information about the Haskell-Cafe