指数默认为整数 [英] Exponents defaulting to Integer
问题描述
我使用(^)::(Num a,Integral b)=> a - > b - >一个
很多来定义常数因子或大小。问题是,GHC抱怨默认为整数
。
现在我知道为什么会发生这种情况......而且我知道我可以写(x ^(y :: Int))
来摆脱警告。但那看起来很丑陋。
同样的事情适用于(^^)::(积分b,分数a) => a - > b - > a
和(**):: Floating a => a - > a - >
任何人都可以很好地解决这个(第一个世界)问题吗?
edit
刚刚发现了这段代码:
align a = 2 ^ ceiling(logBase 2(fromIntegral(sizeOf a)))
这是一个LOC,GHC抱怨在同一行上违约为整数
和 Double
。
你可以用更具体的类型定义你自己的操作符。或者你可以使用更具体的类型来重新定义(^)
运算符,如下所示:
< $(^))
$ b $(^):: Num a => code> import Prelude hiding((^))
import qualified Prelude a - > Int - > a
(^)=(Prelude。^)
x :: Int
x = 2 ^ 3
I use (^) :: (Num a, Integral b) => a -> b -> a
a lot to define constant factors or sizes. Problem is that GHC complains about defaulting to Integer
.
Now I know why this happens ... and I know that I can "just" write (x^(y::Int))
to get rid of the warning. But that looks just "ugly". Otoh living with the warnings is also not a great option.
Same thing applies for (^^) :: (Integral b, Fractional a) => a -> b -> a
and (**) :: Floating a => a -> a -> a
is not usable to me.
Anyone has a nice solution to this (first world) problem?
edit
Just found this gem of code:
alignment a = 2 ^ ceiling (logBase 2 (fromIntegral (sizeOf a)))
This is one LOC and GHC complains about defaulting to Integer
and Double
on the same line.
You could define your own operator with a more specific type. Or you could redefine the (^)
operator with a more specific type, like this:
import Prelude hiding ((^))
import qualified Prelude ((^))
(^) :: Num a => a -> Int -> a
(^) = (Prelude.^)
x :: Int
x = 2^3
这篇关于指数默认为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!