rint :: Int -> Double -> Double rint d x = fromIntegral (round (x * 10^d)) / 10^d
GHC reports
main.hs:442:25: Warning: Defaulting the following constraint(s) to type `Integer' `Integral b' arising from a use of `round' at main.hs: 442:25-40 In the first argument of `fromIntegral', namely `(round (x * 10 ^ d))' In the first argument of `(/)', namely `fromIntegral (round (x * 10 ^ d))' In the expression: fromIntegral (round (x * 10 ^ d)) / 10 ^ d
I tried various signature constraint to remove the warning (-Wall) without success. I remember to have read something about this "defaulting" problem in the Haskell type system so I am not sure that a solution exist.
Any hint? Is there any other solution to truncate a Floating to a given number of digits?
ld <laurent.den...@gmail.com> writes: > main.hs:442:25: > Warning: Defaulting the following constraint(s) to type `Integer' > `Integral b' arising from a use of `round' at main.hs:
It's wondering which integral type to use after rounding but before converting back to Double. For instance,
rint :: Int -> Double -> Double rint d x = fromIntegral (round (x * 10^d) :: Integer) / 10^d
On 4 mar, 15:30, "Mark T. B. Carroll" <Mark.Carr...@Aetion.com> wrote:
> ld <laurent.den...@gmail.com> writes: > > main.hs:442:25: > > Warning: Defaulting the following constraint(s) to type `Integer' > > `Integral b' arising from a use of `round' at main.hs:
> It's wondering which integral type to use after rounding but before > converting back to Double. For instance,
> rint :: Int -> Double -> Double > rint d x = fromIntegral (round (x * 10^d) :: Integer) / 10^d