|
|
|
@ -16,27 +16,30 @@ data Price = Price { |
|
|
|
giga :: Int64 |
|
|
|
giga :: Int64 |
|
|
|
giga = 1000000000 |
|
|
|
giga = 1000000000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mega :: Int64 |
|
|
|
|
|
|
|
mega = 1000000 |
|
|
|
|
|
|
|
|
|
|
|
instance Num Price where |
|
|
|
instance Num Price where |
|
|
|
a + b = Price { |
|
|
|
a + b = Price { |
|
|
|
priceQuants = priceQuants a + priceQuants b } |
|
|
|
priceQuants = priceQuants a + priceQuants b } |
|
|
|
|
|
|
|
|
|
|
|
a * b = Price { |
|
|
|
a * b = Price { |
|
|
|
priceQuants = (priceQuants a * priceQuants b) `div` giga } |
|
|
|
priceQuants = (priceQuants a * priceQuants b) `div` mega } |
|
|
|
|
|
|
|
|
|
|
|
abs a = a { priceQuants = abs (priceQuants a) } |
|
|
|
abs a = a { priceQuants = abs (priceQuants a) } |
|
|
|
|
|
|
|
|
|
|
|
signum a = a { priceQuants = signum (priceQuants a)} |
|
|
|
signum a = a { priceQuants = signum (priceQuants a)} |
|
|
|
|
|
|
|
|
|
|
|
fromInteger int = Price { priceQuants = giga * fromInteger int} |
|
|
|
fromInteger int = Price { priceQuants = mega * fromInteger int} |
|
|
|
|
|
|
|
|
|
|
|
negate a = a { priceQuants = negate (priceQuants a) } |
|
|
|
negate a = a { priceQuants = negate (priceQuants a) } |
|
|
|
|
|
|
|
|
|
|
|
toDouble :: Price -> Double |
|
|
|
toDouble :: Price -> Double |
|
|
|
toDouble p = fromIntegral (priceQuants p) / fromIntegral giga |
|
|
|
toDouble p = fromIntegral (priceQuants p) / fromIntegral mega |
|
|
|
|
|
|
|
|
|
|
|
fromDouble :: Double -> Price |
|
|
|
fromDouble :: Double -> Price |
|
|
|
fromDouble d = Price { priceQuants = truncate (d * fromIntegral giga) } |
|
|
|
fromDouble d = Price { priceQuants = truncate (d * fromIntegral mega) } |
|
|
|
|
|
|
|
|
|
|
|
decompose :: Price -> (Int64, Int32) |
|
|
|
decompose :: Price -> (Int64, Int32) |
|
|
|
decompose Price{priceQuants = p} = (p `div` giga, (fromInteger . toInteger) $ p `mod` giga) |
|
|
|
decompose Price{priceQuants = p} = (p `div` mega, (fromInteger . toInteger) $ p `mod` mega) |
|
|
|
|
|
|
|
|
|
|
|
|