Browse Source

Price: decompose

master
Denis Tereshkin 8 years ago
parent
commit
004f63a18e
  1. 7
      src/ATrade/Price.hs
  2. 5
      test/TestTypes.hs

7
src/ATrade/Price.hs

@ -3,7 +3,8 @@ @@ -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 @@ -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)

5
test/TestTypes.hs

@ -35,6 +35,7 @@ properties = testGroup "Types" [ @@ -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" $ @@ -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)

Loading…
Cancel
Save