]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Amount/Quantity.hs
Correction : compatiblité avec GHC-7.6 en limitant l’usage de Prelude.
[comptalang.git] / lib / Hcompta / Amount / Quantity.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 module Hcompta.Amount.Quantity where
7
8 import Data.Bool
9 import Data.Data
10 #ifdef DOUBLE
11 #else
12 import Data.Decimal
13 #endif
14 import Data.Eq (Eq(..))
15 import Data.String (String)
16 import Data.Word
17 import Data.Typeable ()
18 import Prelude ((.))
19
20 #ifdef DOUBLE
21 type Quantity
22 = Double
23 #else
24 type Quantity
25 = Decimal
26 deriving instance Data Quantity
27 #endif
28
29 representation :: String
30 zero :: Quantity
31 is_zero :: Quantity -> Bool
32 round :: Word8 -> Quantity -> Quantity
33 #ifdef DOUBLE
34 representation = "Double"
35 zero = 0.0
36
37 round n f = fromInteger $ round $ (f * (10^n)) / (10.0^^n)
38
39 is_zero = (== 0) . decimalMantissa
40 --is_zero decimal_places quantity =
41 -- floor quantity == 0 && -- NOTE: check integral part, in case of an overflow in roundTo'
42 -- 0 == roundTo' decimal_places quantity
43 -- where
44 -- roundTo' n f = fromInteger $ round $ f * (10 ^ n)
45
46 #else
47 representation = "Decimal"
48 zero = 0
49
50 round = Data.Decimal.roundTo
51
52 is_zero = (== 0) . decimalMantissa
53 --is_zero decimal_places quantity =
54 -- (== 0) $ decimalMantissa $
55 -- Hcompta.Amount.Quantity.round decimal_places quantity
56 #endif
57