将 IO Double 舍入到指定的位数 - Haskell
Round IO Double to specified number of digits - Haskell
有没有办法四舍五入IO Double
?我正在寻找一个函数:
ownRound :: IO Double -> IO Double
使用这些单元测试:
ownRound 0.51 == 0.5
ownRound 0.49 == 0.5
ownRound 0.5 == 0.5
ownRound 0.7132 == 0.7
ownRound 0.39 == 0.4
你要的东西得不到。您编写的测试用例是针对函数
tensRound :: Double -> Double
但是你写的类型签名是针对函数的
ownRound :: IO Double -> IO Double
如果您打算将测试用例编写为
ownRound (return 0.51) == return 0.5
ownRound (return 0.49) == return 0.5
等等,其中每个数字都包装到 IO 中,那么这些实现将起作用:
tensRound :: Double -> Double
tensRound d = fromInteger (round (d*10)) / 10
ownRound :: IO Double -> IO Double
ownRound = fmap tensRound
有没有办法四舍五入IO Double
?我正在寻找一个函数:
ownRound :: IO Double -> IO Double
使用这些单元测试:
ownRound 0.51 == 0.5
ownRound 0.49 == 0.5
ownRound 0.5 == 0.5
ownRound 0.7132 == 0.7
ownRound 0.39 == 0.4
你要的东西得不到。您编写的测试用例是针对函数
tensRound :: Double -> Double
但是你写的类型签名是针对函数的
ownRound :: IO Double -> IO Double
如果您打算将测试用例编写为
ownRound (return 0.51) == return 0.5
ownRound (return 0.49) == return 0.5
等等,其中每个数字都包装到 IO 中,那么这些实现将起作用:
tensRound :: Double -> Double
tensRound d = fromInteger (round (d*10)) / 10
ownRound :: IO Double -> IO Double
ownRound = fmap tensRound