[Haskell-cafe] about integer and float operations

Manlio Perillo manlio_perillo at libero.it
Wed Feb 4 10:42:03 EST 2009


During some experiments with Python and Haskell I found some important 
differences about how some integer and float operations are implemented.

The first difference is about a `mod` b, when a and b are Float types.
Python use the fmod function, and it also implement divmod; Haskell 
seems to lack support for this operation.

The second difference is about the division of two integers.

Consider this Python code:

 >>> from __future__ import division
 >>> def fac(n):
...     return reduce(lambda a, b: a * (b + 1), xrange(n), 1)
 >>> float(fac(777))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
 >>> fac(777) / fac(777)

Here CPython does not convert the two integers to float before to divide 
them, but make use of a special algorithm.
GHC, instead, returns NaN

I don't know if the implementations of divMod and "true" integer 
division in CPython are "robust", but there is some important reason why 
these operations are not supported in Haskell?

Thanks  Manlio Perillo

More information about the Haskell-Cafe mailing list