Hi guys!
I'm trying to create a small module for doing decimal-based calculations. A number is stored as an integer mantisse, with a precision value specified by an int:
data APNum =
{ getMantisse :: Integer
, getPrecision :: Int }
For instance:
APNum 123 0 -> 123
APNum 123 1 -> 1.23
APNum 123 2 -> 12.3
...
(negative precision is not allowed).
Now I wrote this function, which adjusts the precision automatically by stripping as many trailing zero's as possible:
autoPrecision :: APNum -> APNum
autoPrecision x@(APNum m p) = if p > maxPrecision
then autoPrecision $ setPrecision x maxPrecision
else autoPrecision' m p where
autoPrecision' m p = let (m',r) = m `divMod` 10 in
if r /= 0 || p <= 0 then APNum m p else autoPrecision' m' (pred p)
(MaxPrecision and setPrecision are obvious, I think).
The problem is, this snippet has a very bad performance, specially n numbers with more then 10000 digits. Are there any simple optimizations?