math - Haskell 浮点错误

标签 math haskell pattern-matching complex-numbers

所以我已经完成了在 haskell 中创建自己的复数数据类型。

由于这里的另一个问题,我还得到了一个可以求解二次方程的函数。

现在唯一的问题是,当尝试求解具有复数根的二次方程时,代码会在拥抱中生成解析错误。

即拥抱中...

Main> solve (Q 1 2 1)
(-1.0,-1.0)

Main> solve (Q 1 2 0)
(0.0,-2.0)

Main> solve (Q 1 2 2)
(
Program error: pattern match failure: v1618_v1655 (C -1.#IND -1.#IND)

在我看来,应用平方根后会出现问题,但我真的不确定。任何试图找出问题所在或任何有关此错误含义的指示的帮助都会很棒。

谢谢

托马斯

代码:

-- A complex number z = (re +im.i) is represented as a pair of Floats

data Complex = C {
re :: Float,
im :: Float
} deriving Eq

-- Display complex numbers in the normal way

instance Show Complex where
    show (C r i)
        | i == 0            = show r
        | r == 0            = show i++"i"
        | r < 0 && i < 0    = show r ++ " - "++ show (C 0 (i*(-1)))
        | r < 0 && i > 0    = show r ++ " + "++ show (C 0 i)
        | r > 0 && i < 0    = show r ++ " - "++ show (C 0 (i*(-1)))
        | r > 0 && i > 0    = show r ++ " + "++ show (C 0 i)


-- Define algebraic operations on complex numbers
instance Num Complex where
    fromInteger n       = C (fromInteger n) 0 -- tech reasons
    (C a b) + (C x y)   = C (a+x) (b+y)
    (C a b) * (C x y)   = C (a*x - b*y) (b*x + b*y)
    negate (C a b)      = C (-a) (-b)

instance Fractional Complex where
    fromRational r      = C (fromRational r) 0 -- tech reasons
    recip (C a b)       = C (a/((a^2)+(b^2))) (b/((a^2)+(b^2)))


root :: Complex -> Complex
root (C x y)
    | y == 0 && x == 0  = C 0 0
    | y == 0 && x > 0   = C (sqrt ( ( x + sqrt ( (x^2) + 0 ) ) / 2 ) )  0
    | otherwise         = C (sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ((y/(2*(sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ) ) )


-- quadratic polynomial : a.x^2 + b.x + c
data Quad = Q {
    aCoeff, bCoeff, cCoeff :: Complex
} deriving Eq


instance Show Quad where
    show (Q a b c) = show a ++ "x^2 + " ++ show b ++ "x + " ++ show c

solve :: Quad -> (Complex, Complex)
solve (Q a b c) = ( sol (+), sol (-) )
  where sol op = (op (negate b) $ root $ b*b - 4*a*c) / (2 * a)

最佳答案

您的数字在您的错误中似乎是非规范化的:

(C -1.#IND -1.#IND)

在这种情况下,您不能假设任何浮点比较都有效。这是 float 的定义。然后是你对 show 的定义

show (C r i)
    | i == 0                        = show r
    | r == 0                        = show i++"i"
    | r < 0 && i < 0        = show r ++ " - "++ show (C 0 (i*(-1)))
    | r < 0 && i > 0        = show r ++ " + "++ show (C 0 i)
    | r > 0 && i < 0        = show r ++ " - "++ show (C 0 (i*(-1)))
    | r > 0 && i > 0        = show r ++ " + "++ show (C 0 i)

由于非规范化的数字,留下了模式失败的机会。您可以添加以下条件

    | otherwise = show r ++ "i" ++ show i"

现在当你评估时为什么会这样

b * b - 4 * a * c

使用 Q 1 2 2,您得到 -4,然后在根中,您陷入最后一种情况,在第二个方程中:

              y
-----------------------------
            ________________
           /        _______
          /        / 2    2
         /   x + \/ x  + y
 2 * \  /   ----------------
      \/           2

-4 + sqrt( (-4) ^2) == 0,从那里开始,你就注定了,除以 0,后跟一个“NaN”(不是数字),搞砸其他一切

关于math - Haskell 浮点错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1226687/

相关文章:

math - 带四元数的非线性 slerp 动画

haskell - Haskell 中的级别顺序

Python:Rabin-Karp算法散列

c++ - 在 3d 空间中移动一个点

VB.Net-评估字符串中的数学表达式

haskell - Haskell 的计算机视觉库

haskell - "Illegal view pattern: fromPathPiece -> Just dyn_abDD"在路由上使用参数时

algorithm - 单词模式查找器

python - 在Python中的for循环中匹配字符串

math - float 学有问题吗?