[Haskell-cafe] Implementation of scaled integers
Lennart Augustsson
lennart at augustsson.net
Tue Feb 13 15:33:39 EST 2007
The tricky part to get efficient is multiply and divide.
Say you pick Int32 as the underlying type, when multiplying
you really want the 64 bit result and then scale that.
AFAIK, there are no such primitives exposed to the user.
What you can do is cast to 64 bit, multiply, shift, and cast
back again. It shouldn't be too bad.
As for having the scale factor be part of the type, that shouldn't
be a problem.
-- Lennart
On Feb 13, 2007, at 19:15 , Stefan Heinzmann wrote:
> Hi all,
>
> is there a library for Haskell that implements scaled integers, i.e.
> integers with a fixed scale factor so that the scale factor does not
> need to be stored, but is part of the type?
>
> In particular it would be useful (i.e. for signal processing) to have
> numbers based on Int scaled such that they fall into the range
> [-1.0 ..
> 1.0). Or other scale factors which are powers of 2. Addition and
> subtraction would then map to the ordinary operations for Int, while
> Multiplication and Division would have to apply the scale factor to
> correct the result of normal Int operations (which would be a shift
> operation).
>
> If it doesn't exist yet, have you got ideas how to implement that for
> maximum efficiency?
>
> Cheers
> Stefan
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list