The reason that div
does not return Infinity
is simple--there is no representation for infinity in the Integer
type.
/
returns Infinity
because it follows the IEEE 754 standard (which describes floating point number representations) since the default Fractional
type is Double
. Other languages with floating point numbers (e.g. JavaScript) also exhibit this behavior.
To make mathematicians cringe even more, you get a different result if you divide by negative 0, despite the fact that -0 == 0
for floats:
Prelude> 1/(-0)
-Infinity
This is also behavior from the standard.
If you use a different fractional type like Rational
, you will get the behavior you expect:
Prelude> 1 / (0 :: Rational)
*** Exception: Ratio.%: zero denominator
Coincidentally, if you're wondering about why Integer
and Double
are the types in question when your actual operation does not reference them, take a look at how Haskell handles defaulting types (especially numeric types) in the report.
The short version is that if you have an ambiguous type from the Num
class, Haskell will first try Integer
and then Double
for that type. You can change this with a default (Type1, Type2...)
statement or turn it off with a default ()
statement at the module level.