Reputation: 83527
I have a list areas :: [Double]. Now I want to filter this list for those which are actually integral values. I want to do something like this for my predicate:
isInteger :: Double -> Bool
isInteger x = abs (fromIntegral (floor x) - x) < delta
where delta = 0.00001
However, I would guess there is a better way to do this. Is there a Haskell idiom for checking if a real value is an integer?
Upvotes: 4
Views: 414
Reputation: 152707
This looks fine and idiomatic to me, though you probably want to use round
rather than floor
. You could also consider using approxRational
and checking that the denominator of the result is 1
:
isInteger x = denominator (approxRational x 0.00001) == 1
Upvotes: 7