【问题标题】:Cannot raise Complex Int to power无法将 Complex Int 提升到权力
【发布时间】:2018-12-03 00:02:55
【问题描述】:

似乎将Complex Int 提升为权力在其实例中违反了某些合同。我错了吗?

下面的小例子

compl :: Complex Int -> Maybe (Complex Int)
compl d = (d^) <$> Just 3

还有错误信息:

No instance for (RealFloat Int) arising from an operator section
      In the first argument of ‘(<$>)’, namely ‘(d ^)’
      In the expression: (d ^) <$> Just 3
      In an equation for ‘compl’: compl d = (d ^) <$> Just 3

有没有办法解决这个问题?允许这种类型为浮点数在我的程序中没有任何意义。

【问题讨论】:

    标签: haskell types casting complex-numbers


    【解决方案1】:

    (^)Num 的一个方法。 The Complex type's instantiation of Num 声明为:

    instance RealFloat a => Num (Complex a)
    

    也就是说,您不能将Complex a 视为Num,除非aRealFloat,而Int 不是。请改用Complex Double 等。

    【讨论】:

    • 现在你可能会问为什么不instance Num a =&gt; Num (Complex a)?答案是为复数定义的abs xsignum x 使用x 的大小,这需要RealFloat 实例中的sqrt。例如。 abs (1 :+ 2) = sqrt 5 这不是整数。
    猜你喜欢
    • 1970-01-01
    • 2010-11-15
    • 2015-07-14
    • 2016-04-03
    • 2015-11-13
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多