NaN as Integer value

Hello haskellers, is there a reason why Integer doesn't have 'NaN' as value? I think it would be very convenient to be able to handle (1 `div` 0) as regular NaN value and not as exception. Thanks.

Hello haskellers,is there a reason why Integer doesn't have 'NaN' as value?I think it would be very convenient to be able to handle (1 `div` 0) as regular NaN value and not as exception.Thanks.
I think because, if you need NaN like values, `Maybe Int` does the same job without tainting the definition of `Int`.

Franco answers a question :
Hello haskellers,is there a reason why Integer doesn't have 'NaN' as value?I think it would be very convenient to be able to handle (1 `div` 0) as regular NaN value and not as exception.Thanks. I think because, if you need NaN like values, `Maybe Int` does the same job without tainting the definition of `Int`. This is not a Haskell problem. For Ints, ALL representations are valid numbers, a NaN is a specific float object, unless I'm mistaken, so the introduction of such an abnormal number would require some serious modifications of the representation.
Jerzy Karczmarczuk

You can always use the Maybe type as a follows:
intDiv :: Integer -> Integer -> Maybe Integer
intDiv _ 0 = Nothing
intDiv n m = Just (div n m)
This allows you to pattern match results of divisions:
example :: Integer -> Integer -> Maybe Integer
example n m =
case intDiv 4 n of
Nothing -> Nothing
Just n' ->
case intDiv 5 m of
Nothing -> Nothing
Just m' -> Just (n' + m')
Or even better using the do notation:
example2 :: Integer -> Integer -> Maybe Integer
example2 n m = do
n' <- intDiv 4 n
m' <- intDiv 5 m
return (n' + m')
Note that example and example2 both do the same thing.
I think this is cleaner solution add NaN as a value to the Integer type.
Good luck,
Daniel Díaz.
On Sat, Apr 13, 2013 at 12:16 PM, Алексей Егоров
Hello haskellers,
is there a reason why Integer doesn't have 'NaN' as value? I think it would be very convenient to be able to handle (1 `div` 0) as regular NaN value and not as exception.
Thanks.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- E-mail sent by Daniel Díaz Casanueva let f x = x in x
participants (4)
-
Daniel Díaz Casanueva
-
Franco
-
Jerzy Karczmarczuk
-
Алексей Егоров