1 {-# OPTIONS -Wall -Werror -fno-warn-unused-binds #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) Ashley Yakeley 2005, 2006
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : Ashley Yakeley <ashley@semantic.org>
10 -- Stability : experimental
11 -- Portability : portable
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.
19 -- This module also contains generalisations of div, mod, and divmod to work
20 -- with any Real instance.
22 -----------------------------------------------------------------------------
28 Fixed,HasResolution(..),
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))
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
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
48 newtype Fixed a = MkFixed Integer deriving (Eq,Ord)
50 class HasResolution a where
51 resolution :: a -> Integer
53 fixedResolution :: (HasResolution a) => Fixed a -> Integer
54 fixedResolution fa = resolution (uf fa) where
58 withType :: (a -> f a) -> f a
59 withType foo = foo undefined
61 withResolution :: (HasResolution a) => (Integer -> f a) -> f a
62 withResolution foo = withType (foo . resolution)
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)
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))
83 instance (HasResolution a) => Real (Fixed a) where
84 toRational fa@(MkFixed a) = (toRational a) / (toRational (fixedResolution fa))
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))))
92 instance (HasResolution a) => RealFrac (Fixed a) where
93 properFraction a = (i,a - (fromIntegral i)) where
95 truncate f = truncate (toRational f)
96 round f = round (toRational f)
97 ceiling f = ceiling (toRational f)
98 floor f = floor (toRational f)
100 chopZeros :: Integer -> String
102 chopZeros a | mod a 10 == 0 = chopZeros (div a 10)
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
110 s' = if chopTrailingZeros then chopZeros a else s
112 withDot :: String -> String
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
122 -- enough digits to be unambiguous
123 digits = ceiling (logBase 10 (fromInteger res) :: Double)
125 fracNum = div (d * maxnum) res
127 instance (HasResolution a) => Show (Fixed a) where
128 show = showFixed False
134 instance HasResolution E6 where
135 resolution _ = 1000000
137 type Micro = Fixed E6
142 instance HasResolution E12 where
143 resolution _ = 1000000000000
145 type Pico = Fixed E12