gpt4 book ai didi

haskell - 没有因使用 ‘floor’ 而产生的 (Integral Double) 实例

转载 作者:行者123 更新时间:2023-12-04 02:31:02 27 4
gpt4 key购买 nike

我正在尝试创建一个计算 x mod y 的 Haskell 函数:

fmod :: Double -> Double -> Double
fmod x y = x - y*m
where m = floor a
a = x / y

在堆栈中运行它会产生:

没有因使用“floor”而产生的 (Integral Double) 实例

最佳答案

floor :: (RealFrac a, Integral b) => a -> b 的返回类型是 Integral类型,Double 不是 Integral 类型,因此代码中的 m 不能是 Double,这很重要,因为(*) :: Num a => a -> a -> a(-) :: Num a => a -> a -> a函数返回与两个操作数相同的类型。

但是您可以使用fromInteger :: Num a => Integer -> aInteger 转换为任何 Num 类型。所以你可以使用:

fmod :: Double -> Double -> Double
fmod x y = x - y * <b>fromInteger</b> (floor (x / y))

关于haskell - 没有因使用 ‘floor’ 而产生的 (Integral Double) 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64163033/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com