[Haskell-cafe] about integer and float operations

Max Rabkin max.rabkin at gmail.com
Wed Feb 4 17:08:05 EST 2009


On Wed, Feb 4, 2009 at 2:03 PM, Manlio Perillo <manlio_perillo at libero.it> wrote:
> Max Rabkin ha scritto:
>>
>> [...]
>>
>> Then we can define
>>    (/.) :: (Real a1, Real a2, Fractional a) => a1 -> a2 -> a
>>    x /. y = fromRational $ toRational x / toRational y
>
>> [...]
>>
>> (//) :: (Integral b, Real a, Real a1) => a -> a1 -> b
>> x // y = floor $ toRational x / toRational y
>>
>> Hope that helps,
>> Max
>>
>
> Yes, thanks.
>
> However there is still a *big* problem: it is inefficient.

It sure is. To get the type you asked for, one *has* to go via rationals. But...

> Here is an Haskell version:
> http://paste.pocoo.org/show/102801/
>
> On my system it takes 30 seconds.

You're only dividing integers by integers to get Doubles.

x /. y = fromIntegral x / fromIntegral y

works just fine in this case.

--Max


More information about the Haskell-Cafe mailing list