
Richard O'Keefe ha scritto:
On 5 Feb 2009, at 10:38 am, Manlio Perillo wrote:
I'm looking for an exact integer division that avoids overflows, if possible.
What this sounds like to me is a request that the Prelude function 'fromRational' should work well.
Just found that it actually works well, with Ratio Integer. There is a specialized function in GHC.Float that does the right job.
[...]
You'd actually use scaleFloat; if the difference sp-sq is outside the range of Int the answer is going to be a signed zero or a signed infinity anyway. decodeIntegerAsFloat would sit very well in the RealFloat class alongside its model, decodeFloat. It has other uses. For example, you can use it to compute logarithms of Integers with much less worry about overflow.
By the way, in GHC.Float there is a (private): -- Compute the (floor of the) log of i in base b. -- Simplest way would be just divide i by b until it's smaller then b, -- but that would be very slow! We are just slightly more clever. integerLogBase :: Integer -> Integer -> Int Regards Manlio Perillo