ATrade core infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
996 B

8 years ago
{-# LANGUAGE MultiWayIf #-}
module ATrade.Price (
Price(..),
fromDouble,
8 years ago
toDouble,
decompose
8 years ago
) where
import Data.Int
data Price = Price {
priceQuants :: !Int64
} deriving (Eq, Show, Ord)
giga :: Int64
giga = 1000000000
mega :: Int64
mega = 1000000
8 years ago
instance Num Price where
a + b = Price {
priceQuants = priceQuants a + priceQuants b }
a * b = Price {
priceQuants = (priceQuants a * priceQuants b) `div` mega }
8 years ago
abs a = a { priceQuants = abs (priceQuants a) }
signum a = a { priceQuants = signum (priceQuants a)}
fromInteger int = Price { priceQuants = mega * fromInteger int}
8 years ago
negate a = a { priceQuants = negate (priceQuants a) }
toDouble :: Price -> Double
toDouble p = fromIntegral (priceQuants p) / fromIntegral mega
8 years ago
fromDouble :: Double -> Price
fromDouble d = Price { priceQuants = truncate (d * fromIntegral mega) }
8 years ago
decompose :: Price -> (Int64, Int32)
decompose Price{priceQuants = p} = (p `div` mega, (fromInteger . toInteger) $ p `mod` mega)
8 years ago