add Data.Fixed module
[ghc-base.git] / Data / Fixed.hs
1 {-# OPTIONS -Wall -Werror -fno-warn-unused-binds #-}
2
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  Data.Fixed
6 -- Copyright   :  (c) Ashley Yakeley 2005, 2006
7 -- License     :  BSD-style (see the file libraries/base/LICENSE)
8 -- 
9 -- Maintainer  :  Ashley Yakeley <ashley@semantic.org>
10 -- Stability   :  experimental
11 -- Portability :  portable
12 --
13 --  This module defines a "Fixed" type for fixed-precision arithmetic.
14 --  The parameter to Fixed is any type that's an instance of HasResolution.
15 --  HasResolution has a single method that gives the resolution of the Fixed type.
16 --  Parameter types E6 and E12 (for 10^6 and 10^12) are defined, as well as
17 --  type synonyms for Fixed E6 and Fixed E12.
18 --
19 --  This module also contains generalisations of div, mod, and divmod to work
20 --  with any Real instance.
21 --
22 -----------------------------------------------------------------------------
23
24 module Data.Fixed
25 (
26         div',mod',divMod',
27
28         Fixed,HasResolution(..),
29         showFixed,
30         E6,Micro,
31         E12,Pico
32 ) where
33
34 -- | generalisation of 'div' to any instance of Real
35 div' :: (Real a,Integral b) => a -> a -> b
36 div' n d = floor ((toRational n) / (toRational d))
37
38 -- | generalisation of 'divMod' to any instance of Real
39 divMod' :: (Real a,Integral b) => a -> a -> (b,a)
40 divMod' n d = (f,n - (fromIntegral f) * d) where
41         f = div' n d
42
43 -- | generalisation of 'mod' to any instance of Real
44 mod' :: (Real a) => a -> a -> a
45 mod' n d = n - (fromInteger f) * d where
46         f = div' n d
47
48 newtype Fixed a = MkFixed Integer deriving (Eq,Ord)
49
50 class HasResolution a where
51         resolution :: a -> Integer
52
53 fixedResolution :: (HasResolution a) => Fixed a -> Integer
54 fixedResolution fa = resolution (uf fa) where
55         uf :: Fixed a -> a
56         uf _ = undefined
57
58 withType :: (a -> f a) -> f a
59 withType foo = foo undefined
60
61 withResolution :: (HasResolution a) => (Integer -> f a) -> f a
62 withResolution foo = withType (foo . resolution)
63
64 instance Enum (Fixed a) where
65         succ (MkFixed a) = MkFixed (succ a)
66         pred (MkFixed a) = MkFixed (pred a)
67         toEnum = MkFixed . toEnum
68         fromEnum (MkFixed a) = fromEnum a
69         enumFrom (MkFixed a) = fmap MkFixed (enumFrom a)
70         enumFromThen (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromThen a b)
71         enumFromTo (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromTo a b)
72         enumFromThenTo (MkFixed a) (MkFixed b) (MkFixed c) = fmap MkFixed (enumFromThenTo a b c)
73
74 instance (HasResolution a) => Num (Fixed a) where
75         (MkFixed a) + (MkFixed b) = MkFixed (a + b)
76         (MkFixed a) - (MkFixed b) = MkFixed (a - b)
77         fa@(MkFixed a) * (MkFixed b) = MkFixed (div (a * b) (fixedResolution fa))
78         negate (MkFixed a) = MkFixed (negate a)
79         abs (MkFixed a) = MkFixed (abs a)
80         signum (MkFixed a) = fromInteger (signum a)
81         fromInteger i = withResolution (\res -> MkFixed (i * res))
82
83 instance (HasResolution a) => Real (Fixed a) where
84         toRational fa@(MkFixed a) = (toRational a) / (toRational (fixedResolution fa))
85
86 instance (HasResolution a) => Fractional (Fixed a) where
87         fa@(MkFixed a) / (MkFixed b) = MkFixed (div (a * (fixedResolution fa)) b)
88         recip fa@(MkFixed a) = MkFixed (div (res * res) a) where
89                 res = fixedResolution fa
90         fromRational r = withResolution (\res -> MkFixed (floor (r * (toRational res))))
91
92 instance (HasResolution a) => RealFrac (Fixed a) where
93         properFraction a = (i,a - (fromIntegral i)) where
94                 i = truncate a
95         truncate f = truncate (toRational f)
96         round f = round (toRational f)
97         ceiling f = ceiling (toRational f)
98         floor f = floor (toRational f)
99
100 chopZeros :: Integer -> String
101 chopZeros 0 = ""
102 chopZeros a | mod a 10 == 0 = chopZeros (div a 10)
103 chopZeros a = show a
104
105 -- only works for positive a
106 showIntegerZeros :: Bool -> Int -> Integer -> String
107 showIntegerZeros True _ 0 = ""
108 showIntegerZeros chopTrailingZeros digits a = replicate (digits - length s) '0' ++ s' where
109         s = show a
110         s' = if chopTrailingZeros then chopZeros a else s
111
112 withDot :: String -> String
113 withDot "" = ""
114 withDot s = '.':s
115
116 -- | First arg is whether to chop off trailing zeros
117 showFixed :: (HasResolution a) => Bool -> Fixed a -> String
118 showFixed chopTrailingZeros fa@(MkFixed a) | a < 0 = "-" ++ (showFixed chopTrailingZeros (asTypeOf (MkFixed (negate a)) fa))
119 showFixed chopTrailingZeros fa@(MkFixed a) = (show i) ++ (withDot (showIntegerZeros chopTrailingZeros digits fracNum)) where
120         res = fixedResolution fa
121         (i,d) = divMod a res
122         -- enough digits to be unambiguous
123         digits = ceiling (logBase 10 (fromInteger res) :: Double)
124         maxnum = 10 ^ digits
125         fracNum = div (d * maxnum) res
126
127 instance (HasResolution a) => Show (Fixed a) where
128         show = showFixed False
129
130
131
132 data E6 = E6
133
134 instance HasResolution E6 where
135         resolution _ = 1000000
136
137 type Micro = Fixed E6
138
139
140 data E12 = E12
141
142 instance HasResolution E12 where
143         resolution _ = 1000000000000
144
145 type Pico = Fixed E12