没有 (Floating Int) 实例

发布于 2024-08-16 18:09:40 字数 655 浏览 5 评论 0原文

我正在学习哈斯克尔。我创建了一个函数,该函数返回基数“b”中最多“n”的乘法表。数字被填充为“w”位。作为最后一步,我想自动计算“w”。为什么这不能编译?

-- Number of digits needed for the multiplication table n*n in base 'base'
nOfDg :: Int -> Int-> Int 
nOfDg n base = 1 + floor ( logBase base (n*n)) 

错误:

No instance for (Floating Int)
     arising from a use of `logBase' at C:\haskel\dgnum.hs:4:24-38
   Possible fix: add an instance declaration for (Floating Int)
   In the first argument of `floor', namely `(logBase b (n * n))'
   In the second argument of `(+)', namely `floor (logBase b (n * n))'
   In the expression: 1 + floor (logBase b (n * n))

I am learning Haskell. I have created function which returns multiplication table up to 'n' in base 'b'. Numbers are padded to 'w' digits. As the last step, I want to compute 'w' automatically. Why does this not compile?

-- Number of digits needed for the multiplication table n*n in base 'base'
nOfDg :: Int -> Int-> Int 
nOfDg n base = 1 + floor ( logBase base (n*n)) 

error:

No instance for (Floating Int)
     arising from a use of `logBase' at C:\haskel\dgnum.hs:4:24-38
   Possible fix: add an instance declaration for (Floating Int)
   In the first argument of `floor', namely `(logBase b (n * n))'
   In the second argument of `(+)', namely `floor (logBase b (n * n))'
   In the expression: 1 + floor (logBase b (n * n))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思念满溢 2024-08-23 18:09:40

logBase 采用两个实现浮动类型类的参数。在将参数传递给 logBase 之前,您需要对参数调用 fromIntegral。这是用 6.10.3 为我编译的:

nOfDg :: Int -> Int-> Int
nOfDg n base = 1 + floor ( logBase (fromIntegral base) (fromIntegral (n*n)))

你必须记住 Haskell 是非常强类型的,所以你不能仅仅假设提供给你的函数的 Int 参数会自动强制转换为 log 函数通常采用的浮点数。

logBase takes two parameters which implement the floating typeclass. You'll need to call fromIntegral on the parameters before passing them to logBase. This compiled for me with 6.10.3:

nOfDg :: Int -> Int-> Int
nOfDg n base = 1 + floor ( logBase (fromIntegral base) (fromIntegral (n*n)))

You have to remember that Haskell is very strongly typed, so you can't just assume that the Int parameters supplied to your function will automatically be coerced to the floating numbers that log functions generally take.

蓝戈者 2024-08-23 18:09:40

logBase 被声明为适用于浮点类型。 Int 不是浮点类型,Haskell 中没有自动转换。试试这个:

-- Number of digits needed for the multiplication table n*n in base 'base'
nOfDg :: Int -> Float -> Int
nOfDg n base = 1 + floor (logBase base (fromIntegral (n*n)))

logBase is declared to work on floating point types. Int is not a floating point type and there is no automatic conversion in Haskell. Try this:

-- Number of digits needed for the multiplication table n*n in base 'base'
nOfDg :: Int -> Float -> Int
nOfDg n base = 1 + floor (logBase base (fromIntegral (n*n)))
就像说晚安 2024-08-23 18:09:40

从序言中:

logBase :: Floating a => a -> a -> a

这意味着使用 logBase 您必须使用浮动类型。但 Int 不是浮点类型,并且没有数字类型的自动转换,因此必须将其从 Int 转换为 Floating 类型:

nOfDg n base = 1 + floor ( logBase (toEnum base) (toEnum n))

toEnum 函数以 int 作为参数并返回“Enum”类型。好的部分是 Float 是 Enum 的一个实例,所以你可以使用它

toEnum :: Enum a => Int -> a

你应该阅读/记录 Haskell 中数字类型的标准类型类(Num、Fractional、Integral、Floating...),因为它们经常弹出在代码中,学习转换可能很有用。

编辑:这本Haskell Wiki Book提供了标准类型类之间关系的非常有用的图形,包括数字类型。

From the Prelude :

logBase :: Floating a => a -> a -> a

It means that using logBase you must be using a floating type. But Int is not a floating Type, and there is no automatic conversion for numeric types, so you have to convert it from Int to a Floating type :

nOfDg n base = 1 + floor ( logBase (toEnum base) (toEnum n))

the toEnum function take an int as parameter and return an "Enum" type. The good part is that Float is an instance of Enum, so you could use it

toEnum :: Enum a => Int -> a

You should read/document you about standard type classes in haskell for numeric types (Num,Fractional,Integral, Floating...) as they often pop-up in code, learning conversions could be useful.

Edit : this Haskell Wiki Book provide a very useful graphic of relationship between standards type class, including numeric types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文