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.

43 lines
966 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
instance Num Price where
a + b = Price {
priceQuants = priceQuants a + priceQuants b }
a * b = Price {
priceQuants = (priceQuants a * priceQuants b) `div` giga }
abs a = a { priceQuants = abs (priceQuants a) }
signum a = a { priceQuants = signum (priceQuants a)}
fromInteger int = Price { priceQuants = giga * fromInteger int}
negate a = a { priceQuants = negate (priceQuants a) }
toDouble :: Price -> Double
toDouble p = fromIntegral (priceQuants p) / fromIntegral giga
fromDouble :: Double -> Price
fromDouble d = Price { priceQuants = truncate (d * fromIntegral giga) }
8 years ago
decompose :: Price -> (Int64, Int32)
decompose Price{priceQuants = p} = (p `div` giga, (fromInteger . toInteger) $ p `mod` giga)