Decimal 0.1.0 → 0.2.0
raw patch · 4 files changed
+43/−56 lines, 4 filesdep ~QuickCheckdep ~base
Dependency ranges changed: QuickCheck, base
Files
- Decimal.cabal +5/−5
- INSTALL.txt +0/−14
- src/Data/Decimal.hs +22/−21
- tests/Main.hs +16/−16
Decimal.cabal view
@@ -1,19 +1,19 @@ Name: Decimal-Version: 0.1.0+Version: 0.2.0 License: BSD3 License-file: LICENSE.txt-Copyright: Paul Johnson, 2008+Copyright: Paul Johnson, 2011 Author: Paul Johnson Maintainer: paul@cogito.org.uk Stability: beta Category: Math-Build-Depends: base, QuickCheck, HUnit+Build-Depends: base==4, QuickCheck>2.4, HUnit Build-type: Simple Synopsis: Decimal numbers with variable precision Description: A decimal number has an integer mantissa and a negative exponent. The exponent can be interpreted as the number of decimal places in the value. Exposed-modules: Data.Decimal-Extra-source-files: tests/Makefile, tests/Main.hs, README.txt, INSTALL.txt+Extra-source-files: tests/Makefile, tests/Main.hs, README.txt ghc-options: -Wall-hs-source-dirs: src+hs-source-dirs: src,tests
− INSTALL.txt
@@ -1,14 +0,0 @@-As user, from within the project directory:-- runghc Setup.hs configure- runghc Setup.hs build--As root or administrator:-- runghc Setup.hs install--If you have Haddock then generate the documentation with:-- runghc Setup.hs haddock--If you use Hugs then type "runhugs" instead of "runghc".
src/Data/Decimal.hs view
@@ -44,7 +44,6 @@ ) where -import Control.Monad import Data.Char import Data.Ratio import Data.Word@@ -68,8 +67,8 @@ -- overflow in the internal algorithms. However the result of each operator -- will be converted to the mantissa type without checking for overflow. data (Integral i) => DecimalRaw i = Decimal {- decimalPlaces :: Word8,- decimalMantissa :: i}+ decimalPlaces :: {-# UNPACK #-} ! Word8,+ decimalMantissa :: {-# UNPACK #-} ! i} -- | Arbitrary precision decimal type. As a rule programs should do decimal@@ -139,7 +138,7 @@ optional $ char '-' munch1 isDigit fractPart <- option "" $ do- char '.'+ _ <- char '.' munch1 isDigit return $ Decimal (fromIntegral $ length fractPart) $ read $ intPart ++ fractPart@@ -173,8 +172,10 @@ e <- sized (\n -> resize (n `div` 10) arbitrary) :: Gen Int m <- sized (\n -> resize (n * 10) arbitrary) return $ Decimal (fromIntegral $ abs e) m- coarbitrary (Decimal e m) gen = - variant (fromIntegral e + fromIntegral m) gen+ +instance (Integral i, Arbitrary i) => CoArbitrary (DecimalRaw i) where+ coarbitrary (Decimal e m) gen = variant (v:: Integer) gen+ where v = fromIntegral e + fromIntegral m -- | Divide a @DecimalRaw@ value into one or more portions. The portions@@ -205,7 +206,7 @@ -- -- > let result = allocate d parts -- > in all (== d / sum parts) $ zipWith (/) result parts-allocate :: (Integral i) => DecimalRaw i -> [Int] -> [DecimalRaw i]+allocate :: (Integral i) => DecimalRaw i -> [Integer] -> [DecimalRaw i] allocate (Decimal e n) ps | total == 0 = error "Data.Decimal.allocate: allocation list must not sum to zero."@@ -282,38 +283,38 @@ -- | Multiplication is repeated addition. -- --- > (sum $ replicate i d) == d * fromIntegral (max i 0)-prop_repeatedAdd :: Decimal -> Int -> Bool-prop_repeatedAdd d i = (sum $ replicate i d) == d * fromIntegral (max i 0)+-- > forall d, NonNegative i : (sum $ replicate i d) == d * fromIntegral (max i 0)+prop_repeatedAdd :: Decimal -> Word8 -> Bool+prop_repeatedAdd d i = (sum $ replicate (fromIntegral i) d) == d * fromIntegral (max i 0) -- | Division produces the right number of parts. -- --- > i > 0 ==> (sum $ map fst $ divide d i) == i-prop_divisionParts :: Decimal -> Int -> Property-prop_divisionParts d i = i > 0 ==> (sum $ map fst $ divide d i) == i+-- > forall d, Positive i : (sum $ map fst $ divide d i) == i+prop_divisionParts :: Decimal -> Positive Int -> Property+prop_divisionParts d (Positive i) = i > 0 ==> (sum $ map fst $ divide d i) == i -- | Division doesn't drop any units. -- --- > i > 0 ==> (sum $ map (\(n,d1) -> fromIntegral n * d1) $ divide d i) == d-prop_divisionUnits :: Decimal -> Int -> Property-prop_divisionUnits d i = - i > 0 ==> (sum $ map (\(n,d1) -> fromIntegral n * d1) $ divide d i) == d+-- > forall d, Positive i : (sum $ map (\(n,d1) -> fromIntegral n * d1) $ divide d i) == d+prop_divisionUnits :: Decimal -> Positive Int -> Bool+prop_divisionUnits d (Positive i) = + (sum $ map (\(n,d1) -> fromIntegral n * d1) $ divide d i) == d -- | Allocate produces the right number of parts. -- --- > (not . null) ps ==> length ps == length (allocate d ps)-prop_allocateParts :: Decimal -> [Int] -> Property+-- > sum ps /= 0 ==> length ps == length (allocate d ps)+prop_allocateParts :: Decimal -> [Integer] -> Property prop_allocateParts d ps = sum ps /= 0 ==> length ps == length (allocate d ps) -- | Allocate doesn't drop any units. -- --- > (not . null) ps ==> sum (allocate d ps) == d-prop_allocateUnits :: Decimal -> [Int] -> Property+-- > sum ps /= 0 ==> sum (allocate d ps) == d+prop_allocateUnits :: Decimal -> [Integer] -> Property prop_allocateUnits d ps = sum ps /= 0 ==> sum (allocate d ps) == d
tests/Main.hs view
@@ -6,42 +6,42 @@ import Test.QuickCheck -conf :: Config-conf = defaultConfig { configMaxTest = 1000, configMaxFail = 10000 }+conf :: Args+conf = stdArgs {maxSuccess = 1000} main :: IO () main = do putStrLn "QuickCheck Data.Decimal:" putStr " * prop_readShow "- check conf $ prop_readShow+ quickCheckWith conf $ prop_readShow putStr " * prop_readShowPrecision "- check conf $ prop_readShowPrecision+ quickCheckWith conf $ prop_readShowPrecision putStr " * prop_fromIntegerZero "- check conf $ prop_fromIntegerZero+ quickCheckWith conf $ prop_fromIntegerZero putStr " * prop_increaseDecimals "- check conf $ prop_increaseDecimals+ quickCheckWith conf $ prop_increaseDecimals putStr " * prop_decreaseDecimals "- check conf $ prop_decreaseDecimals+ quickCheckWith conf $ prop_decreaseDecimals putStr " * prop_inverseAdd "- check conf $ prop_inverseAdd+ quickCheckWith conf $ prop_inverseAdd putStr " * prop_repeatedAdd "- check conf $ prop_repeatedAdd+ quickCheckWith conf $ prop_repeatedAdd putStr " * prop_divisionParts "- check conf $ prop_divisionParts+ quickCheckWith conf $ prop_divisionParts putStr " * prop_divisionUnits "- check conf $ prop_divisionUnits+ quickCheckWith conf $ prop_divisionUnits putStr " * prop_allocateParts "- check conf $ prop_allocateParts+ quickCheckWith conf $ prop_allocateParts putStr " * prop_allocateUnits "- check conf $ prop_allocateUnits+ quickCheckWith conf $ prop_allocateUnits putStr " * prop_abs "- check conf $ prop_abs+ quickCheckWith conf $ prop_abs putStr " * prop_signum "- check conf $ prop_signum+ quickCheckWith conf $ prop_signum putStrLn "Point tests:"- runTestTT $ TestList+ _ <- runTestTT $ TestList [ TestCase $ assertEqual "pi to 3dp" (Decimal 3 3142 :: Decimal) (realFracToDecimal 3 (pi :: Double)),