【发布时间】:2010-08-18 04:48:34
【问题描述】:
我正在尝试创建一个小模块来进行基于十进制的计算。数字存储为整数尾数,精度值由 int 指定:
data APNum =
{ getMantisse :: Integer
, getPrecision :: Int }
例如:
APNum 123 0 -> 123
APNum 123 1 -> 1.23
APNum 123 2 -> 12.3
...
(不允许负精度)。
现在我编写了这个函数,它通过去除尽可能多的尾随零来自动调整精度:
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 和 setPrecision 很明显)。
问题是,这个sn-p的性能很差,特别是n个超过10000位的数字。有没有简单的优化?
【问题讨论】:
-
“前导零”是指“尾随零”吗? (即
APNum 12000 5->APNum 12 2) -
@KennyTM 这是我的假设,因为整数不能有前导零
标签: optimization haskell integer