“需要积分浮点实例”错误
具有以下功能的文件:
type Point = (Float, Float)
type Circle = (Float, Float, Float)
getCircle :: Point -> Point -> Point -> Circle
getCircle (a, b) (c, d) (e, f) = (x, y, r)
where
x = ((a^2+b^2)*(f-d) + (c^2+d^2)*(b-f) + (e^2+f^2)*(d-b)) `div` (a*(f-d)+c*(b-f)+e*(d-b)) `div` 2
y = ((a^2+b^2)*(e-c) + (c^2+d^2)*(a-e) + (e^2+f^2)*(c-a)) `div` (b*(e-c)+d*(a-e)+f*(c-a)) `div` 2
r = sqrt ((a-x)^2 + (b-y)^2)
当我尝试将其加载到 Hugs 时抛出错误:
ERROR "/Users/ak/Desktop/1.hs":4 - getCircle 定义所需的 Integral Float 实例
问题的本质是什么?如何解决? 谢谢。
The file with following function:
type Point = (Float, Float)
type Circle = (Float, Float, Float)
getCircle :: Point -> Point -> Point -> Circle
getCircle (a, b) (c, d) (e, f) = (x, y, r)
where
x = ((a^2+b^2)*(f-d) + (c^2+d^2)*(b-f) + (e^2+f^2)*(d-b)) `div` (a*(f-d)+c*(b-f)+e*(d-b)) `div` 2
y = ((a^2+b^2)*(e-c) + (c^2+d^2)*(a-e) + (e^2+f^2)*(c-a)) `div` (b*(e-c)+d*(a-e)+f*(c-a)) `div` 2
r = sqrt ((a-x)^2 + (b-y)^2)
Throws an error when I try to load it to Hugs:
ERROR "/Users/ak/Desktop/1.hs":4 - Instance of Integral Float required for definition of getCircle
What's the essence of the problem and how can it be fixed?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
/
代替div
-- 5 / 2,而不是 5div
2. 原因是 haskell 处理整数和浮点类型不同——它们是不同类型类的实例。(/)
在类型类Fractional
中声明,而div
在类型类Integral
中声明。这些类型类有一个共同的祖先Num
,但除此之外它们没有任何子类型关系。You should use
/
in place ofdiv
-- 5 / 2, not 5div
2. The reason is that haskell treats integral and floating point types differently -- they are instances of different typeclasses.(/)
is declared in typeclassFractional
, whereasdiv
is declared in typeclassIntegral
. Those type classes have a common ancestorNum
, but they do not have any subtyping relationships apart from that.div
是整数除法,因此仅适用于Integral
实例。只需使用/
div
is integer division and thus just works onIntegral
instanves. Simply use/