[Haskell-cafe] about integer and float operations
Manlio Perillo
manlio_perillo at libero.it
Thu Feb 5 05:51:03 EST 2009
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
More information about the Haskell-Cafe
mailing list