module Data.Decimal (
DecimalRaw (..),
Decimal,
realFracToDecimal,
decimalConvert,
unsafeDecimalConvert,
roundTo,
roundTo',
(*.),
divide,
allocate,
eitherFromRational,
normalizeDecimal
) where
import Control.DeepSeq
import Data.Char
import Data.Ratio
import Data.Word
import Data.Typeable
import Text.ParserCombinators.ReadP
data DecimalRaw i = Decimal {
decimalPlaces :: ! Word8,
decimalMantissa :: ! i}
deriving (Typeable)
type Decimal = DecimalRaw Integer
instance (NFData i) => NFData (DecimalRaw i) where
rnf (Decimal _ i) = rnf i
instance (Integral i) => Enum (DecimalRaw i) where
succ x = x + 1
pred x = x 1
toEnum = fromIntegral
fromEnum = fromIntegral . decimalMantissa . roundTo 0
enumFrom = iterate (+1)
enumFromThen x1 x2 = let dx = x2 x1 in iterate (+dx) x1
enumFromTo x1 x2 = takeWhile (<= x2) $ iterate (+1) x1
enumFromThenTo x1 x2 x3 = takeWhile (<= x3) $ enumFromThen x1 x2
realFracToDecimal :: (Integral i, RealFrac r) => Word8 -> r -> DecimalRaw i
realFracToDecimal e r = Decimal e $ round (r * (10^e))
divRound :: (Integral a) => a -> a -> a
divRound n1 n2 = n + bankers
where
(n, r) = n1 `quotRem` n2
bankers = case compare (abs r * 2) (abs n2) of
LT -> 0
GT -> signum n1
EQ -> if odd n then signum n1 else 0
unsafeDecimalConvert :: (Integral a, Integral b) => DecimalRaw a -> DecimalRaw b
unsafeDecimalConvert (Decimal e n) = Decimal e $ fromIntegral n
decimalConvert :: (Integral a, Integral b, Bounded b) =>
DecimalRaw a -> Maybe (DecimalRaw b)
decimalConvert (Decimal e n) =
let n1 :: Integer
n1 = fromIntegral n
n2 = fromIntegral n
ub = fromIntegral $ max maxBound n2
lb = fromIntegral $ min minBound n2
in if lb <= n1 && n1 <= ub then Just $ Decimal e n2 else Nothing
roundTo :: (Integral i) => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo d (Decimal _ 0) = Decimal d 0
roundTo d (Decimal e n) = Decimal d $ fromIntegral n1
where
n1 = case compare d e of
LT -> n `divRound` divisor
EQ -> n
GT -> n * multiplier
divisor = 10 ^ (ed)
multiplier = 10 ^ (de)
roundTo' :: (Integral i) => (Rational -> i) -> Word8 -> DecimalRaw i -> DecimalRaw i
roundTo' _ d (Decimal _ 0) = Decimal d 0
roundTo' f d (Decimal e n) = Decimal d $ f n1
where
divisor = 10 ^ (ed)
multiplier = 10 ^ (de)
n1 = case compare d e of
LT -> toRational n / divisor
EQ -> toRational n
GT -> toRational n * multiplier
roundMax :: (Integral i) => DecimalRaw i -> DecimalRaw i -> (Word8, i, i)
roundMax (Decimal _ 0) (Decimal _ 0) = (0,0,0)
roundMax (Decimal e1 n1) (Decimal _ 0) = (e1,n1,0)
roundMax (Decimal _ 0) (Decimal e2 n2) = (e2,0,n2)
roundMax d1@(Decimal e1 n1) d2@(Decimal e2 n2)
| e1 == e2 = (e1, n1, n2)
| otherwise = (e, n1', n2')
where
e = max e1 e2
(Decimal _ n1') = roundTo e d1
(Decimal _ n2') = roundTo e d2
instance (Integral i, Show i) => Show (DecimalRaw i) where
showsPrec _ (Decimal e n)
| e == 0 = ((signStr ++ strN) ++)
| otherwise = (concat [signStr, intPart, ".", fracPart] ++)
where
strN = show $ abs n
signStr = if n < 0 then "-" else ""
len = length strN
padded = replicate (fromIntegral e + 1 len) '0' ++ strN
(intPart, fracPart) = splitAt (max 1 (len fromIntegral e)) padded
instance (Integral i, Read i) => Read (DecimalRaw i) where
readsPrec _ = readP_to_S readDecimalP
readDecimalP :: (Integral i, Read i) => ReadP (DecimalRaw i)
readDecimalP = do
skipSpaces
s1 <- myOpt '+' $ char '-' +++ char '+'
intPart <- munch1 isDigit
fractPart <- myOpt "" $ do
_ <- char '.'
munch1 isDigit
expPart <- myOpt 0 $ do
_ <- char 'e' +++ char 'E'
s2 <- myOpt '+' $ char '-' +++ char '+'
fmap (applySign s2 . strToInt) $ munch1 isDigit
let n = applySign s1 $ strToInt $ intPart ++ fractPart
e = length fractPart expPart
if e < 0
then return $ Decimal 0 $ n * 10 ^ negate e
else if e < 256
then return $ Decimal (fromIntegral e) n
else pfail
where
strToInt :: (Integral n) => String -> n
strToInt = foldl (\t v -> 10 * t + v) 0 . map (fromIntegral . subtract (ord '0') . ord)
applySign '-' v = negate v
applySign _ v = v
myOpt d p = p <++ return d
instance (Integral i) => Eq (DecimalRaw i) where
d1 == d2 = n1 == n2 where (_, n1, n2) = roundMax d1 d2
instance (Integral i) => Ord (DecimalRaw i) where
compare d1 d2 = compare n1 n2 where (_, n1, n2) = roundMax d1 d2
instance (Integral i) => Num (DecimalRaw i) where
(Decimal _ 0) + d = d
d + (Decimal _ 0) = d
d1 + d2 = Decimal e $ fromIntegral (n1 + n2)
where (e, n1, n2) = roundMax d1 d2
(Decimal _ 0) (Decimal e n) = Decimal e (n)
d (Decimal _ 0) = d
d1 d2 = Decimal e $ fromIntegral (n1 n2)
where (e, n1, n2) = roundMax d1 d2
(Decimal _ 0) * _ = 0
_ * (Decimal _ 0) = 0
d1 * d2 = normalizeDecimal $ realFracToDecimal maxBound $ toRational d1 * toRational d2
abs (Decimal e n) = Decimal e $ abs n
signum (Decimal _ n) = fromIntegral $ signum n
fromInteger n = Decimal 0 $ fromIntegral n
instance (Integral i) => Real (DecimalRaw i) where
toRational (Decimal e n) = fromIntegral n % (10 ^ e)
instance (Integral i) => Fractional (DecimalRaw i) where
fromRational r =
let
v :: Decimal
v = normalizeDecimal $ realFracToDecimal maxBound r
in unsafeDecimalConvert v
a / b = fromRational $ toRational a / toRational b
instance (Integral i) => RealFrac (DecimalRaw i) where
properFraction a = (rnd, fromRational rep)
where
(rnd, rep) = properFraction $ toRational a
divide :: Decimal -> Int -> [(Int, Decimal)]
divide (Decimal e n) d
| d > 0 =
case n `divMod` fromIntegral d of
(result, 0) -> [(d, Decimal e result)]
(result, r) -> [(d fromIntegral r,
Decimal e result),
(fromIntegral r, Decimal e (result+1))]
| otherwise = error "Data.Decimal.divide: Divisor must be > 0."
allocate :: Decimal -> [Integer] -> [Decimal]
allocate (Decimal e n) ps
| total == 0 =
error "Data.Decimal.allocate: allocation list must not sum to zero."
| otherwise = map (Decimal e) $ zipWith () ts (tail ts)
where
ts = map fst $ scanl nxt (n, total) ps
nxt (n1, t1) p1 = (n1 (n1 * p1) `zdiv` t1, t1 p1)
zdiv 0 0 = 0
zdiv x y = x `divRound` y
total = sum ps
(*.) :: (Integral i, RealFrac r) => DecimalRaw i -> r -> DecimalRaw i
(Decimal e m) *. d = Decimal e $ round $ fromIntegral m * d
factorN :: (Integral a)
=> a
-> a
-> (a, a)
factorN d val = factorN' val 0
where
factorN' 1 acc = (acc, 1)
factorN' v acc = if md == 0
then factorN' vd (acc + 1)
else (acc, v)
where
(vd, md) = v `divMod` d
eitherFromRational :: (Integral i) => Rational -> Either String (DecimalRaw i)
eitherFromRational r = if done == 1
then do
wres <- we
return $ Decimal wres (fromIntegral m)
else Left $ show r ++ " has no decimal denominator"
where
den = denominator r
num = numerator r
(f2, rest) = factorN 2 den
(f5, done) = factorN 5 rest
e = max f2 f5
m = num * ((10^e) `div` den)
we = if e > fromIntegral (maxBound :: Word8)
then Left $ show e ++ " is too big ten power to represent as Decimal"
else Right $ fromIntegral e
normalizeDecimal :: (Integral i) => DecimalRaw i -> DecimalRaw i
normalizeDecimal r = case eitherFromRational $ toRational r of
Right x -> x
Left e -> error $ "Impossible happened: " ++ e