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 @@
module ATrade.Price ( module ATrade.Price (
Price(..), Price(..),
fromDouble, fromDouble,
toDouble toDouble,
decompose
) where ) where
import Data.Int import Data.Int
@ -35,3 +36,7 @@ toDouble p = fromIntegral (priceQuants p) / fromIntegral giga
fromDouble :: Double -> Price fromDouble :: Double -> Price
fromDouble d = Price { priceQuants = truncate (d * fromIntegral giga) } 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" [
, testTradeSerialization , testTradeSerialization
, testPrice1 , testPrice1
, testPrice2 , testPrice2
, testPriceDecompose
] ]
testTickSerialization = QC.testProperty "Deserialize serialized tick" 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 QC.forAll (arbitrary `suchThat` (< 1000000000)) (\d -> let newd = (P.toDouble . P.fromDouble) d in
(abs (newd - d) < 0.000001)) (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