diff --git a/src/ATrade/Price.hs b/src/ATrade/Price.hs index 878711b..19e70bb 100644 --- a/src/ATrade/Price.hs +++ b/src/ATrade/Price.hs @@ -3,7 +3,8 @@ module ATrade.Price ( Price(..), fromDouble, - toDouble + toDouble, + decompose ) where import Data.Int @@ -35,3 +36,7 @@ toDouble p = fromIntegral (priceQuants p) / fromIntegral giga fromDouble :: Double -> Price fromDouble d = Price { priceQuants = truncate (d * fromIntegral giga) } + +decompose :: Price -> (Int64, Int32) +decompose Price{priceQuants = p} = (p `div` giga, (fromInteger . toInteger) $ p `mod` giga) + diff --git a/test/TestTypes.hs b/test/TestTypes.hs index a87f86d..f3c0216 100644 --- a/test/TestTypes.hs +++ b/test/TestTypes.hs @@ -35,6 +35,7 @@ properties = testGroup "Types" [ , testTradeSerialization , testPrice1 , testPrice2 + , testPriceDecompose ] testTickSerialization = QC.testProperty "Deserialize serialized tick" @@ -92,3 +93,7 @@ testPrice2 = QC.testProperty "toDouble . fromDouble $ Price" $ QC.forAll (arbitrary `suchThat` (< 1000000000)) (\d -> let newd = (P.toDouble . P.fromDouble) d in (abs (newd - d) < 0.000001)) +testPriceDecompose = QC.testProperty "Price decompose" + (\p -> let (i, f) = decompose p in + i * 1000000000 + (fromInteger . fromIntegral) f == priceQuants p) +