-- | generalisation of 'divMod' to any instance of Real
divMod' :: (Real a,Integral b) => a -> a -> (b,a)
divMod' n d = (f,n - (fromIntegral f) * d) where
-- | generalisation of 'divMod' to any instance of Real
divMod' :: (Real a,Integral b) => a -> a -> (b,a)
divMod' n d = (f,n - (fromIntegral f) * d) where
-- | generalisation of 'mod' to any instance of Real
mod' :: (Real a) => a -> a -> a
mod' n d = n - (fromInteger f) * d where
-- | generalisation of 'mod' to any instance of Real
mod' :: (Real a) => a -> a -> a
mod' n d = n - (fromInteger f) * d where
newtype Fixed a = MkFixed Integer deriving (Eq,Ord)
class HasResolution a where
newtype Fixed a = MkFixed Integer deriving (Eq,Ord)
class HasResolution a where
fixedResolution :: (HasResolution a) => Fixed a -> Integer
fixedResolution fa = resolution (uf fa) where
fixedResolution :: (HasResolution a) => Fixed a -> Integer
fixedResolution fa = resolution (uf fa) where
- succ (MkFixed a) = MkFixed (succ a)
- pred (MkFixed a) = MkFixed (pred a)
- toEnum = MkFixed . toEnum
- fromEnum (MkFixed a) = fromEnum a
- enumFrom (MkFixed a) = fmap MkFixed (enumFrom a)
- enumFromThen (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromThen a b)
- enumFromTo (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromTo a b)
- enumFromThenTo (MkFixed a) (MkFixed b) (MkFixed c) = fmap MkFixed (enumFromThenTo a b c)
+ succ (MkFixed a) = MkFixed (succ a)
+ pred (MkFixed a) = MkFixed (pred a)
+ toEnum = MkFixed . toEnum
+ fromEnum (MkFixed a) = fromEnum a
+ enumFrom (MkFixed a) = fmap MkFixed (enumFrom a)
+ enumFromThen (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromThen a b)
+ enumFromTo (MkFixed a) (MkFixed b) = fmap MkFixed (enumFromTo a b)
+ enumFromThenTo (MkFixed a) (MkFixed b) (MkFixed c) = fmap MkFixed (enumFromThenTo a b c)
- (MkFixed a) + (MkFixed b) = MkFixed (a + b)
- (MkFixed a) - (MkFixed b) = MkFixed (a - b)
- fa@(MkFixed a) * (MkFixed b) = MkFixed (div (a * b) (fixedResolution fa))
- negate (MkFixed a) = MkFixed (negate a)
- abs (MkFixed a) = MkFixed (abs a)
- signum (MkFixed a) = fromInteger (signum a)
- fromInteger i = withResolution (\res -> MkFixed (i * res))
+ (MkFixed a) + (MkFixed b) = MkFixed (a + b)
+ (MkFixed a) - (MkFixed b) = MkFixed (a - b)
+ fa@(MkFixed a) * (MkFixed b) = MkFixed (div (a * b) (fixedResolution fa))
+ negate (MkFixed a) = MkFixed (negate a)
+ abs (MkFixed a) = MkFixed (abs a)
+ signum (MkFixed a) = fromInteger (signum a)
+ fromInteger i = withResolution (\res -> MkFixed (i * res))
- fa@(MkFixed a) / (MkFixed b) = MkFixed (div (a * (fixedResolution fa)) b)
- recip fa@(MkFixed a) = MkFixed (div (res * res) a) where
- res = fixedResolution fa
- fromRational r = withResolution (\res -> MkFixed (floor (r * (toRational res))))
+ fa@(MkFixed a) / (MkFixed b) = MkFixed (div (a * (fixedResolution fa)) b)
+ recip fa@(MkFixed a) = MkFixed (div (res * res) a) where
+ res = fixedResolution fa
+ fromRational r = withResolution (\res -> MkFixed (floor (r * (toRational res))))
- properFraction a = (i,a - (fromIntegral i)) where
- i = truncate a
- truncate f = truncate (toRational f)
- round f = round (toRational f)
- ceiling f = ceiling (toRational f)
- floor f = floor (toRational f)
+ properFraction a = (i,a - (fromIntegral i)) where
+ i = truncate a
+ truncate f = truncate (toRational f)
+ round f = round (toRational f)
+ ceiling f = ceiling (toRational f)
+ floor f = floor (toRational f)
showIntegerZeros :: Bool -> Int -> Integer -> String
showIntegerZeros True _ 0 = ""
showIntegerZeros chopTrailingZeros digits a = replicate (digits - length s) '0' ++ s' where
showIntegerZeros :: Bool -> Int -> Integer -> String
showIntegerZeros True _ 0 = ""
showIntegerZeros chopTrailingZeros digits a = replicate (digits - length s) '0' ++ s' where
showFixed :: (HasResolution a) => Bool -> Fixed a -> String
showFixed chopTrailingZeros fa@(MkFixed a) | a < 0 = "-" ++ (showFixed chopTrailingZeros (asTypeOf (MkFixed (negate a)) fa))
showFixed chopTrailingZeros fa@(MkFixed a) = (show i) ++ (withDot (showIntegerZeros chopTrailingZeros digits fracNum)) where
showFixed :: (HasResolution a) => Bool -> Fixed a -> String
showFixed chopTrailingZeros fa@(MkFixed a) | a < 0 = "-" ++ (showFixed chopTrailingZeros (asTypeOf (MkFixed (negate a)) fa))
showFixed chopTrailingZeros fa@(MkFixed a) = (show i) ++ (withDot (showIntegerZeros chopTrailingZeros digits fracNum)) where
- res = fixedResolution fa
- (i,d) = divMod a res
- -- enough digits to be unambiguous
- digits = ceiling (logBase 10 (fromInteger res) :: Double)
- maxnum = 10 ^ digits
- fracNum = div (d * maxnum) res
+ res = fixedResolution fa
+ (i,d) = divMod a res
+ -- enough digits to be unambiguous
+ digits = ceiling (logBase 10 (fromInteger res) :: Double)
+ maxnum = 10 ^ digits
+ fracNum = div (d * maxnum) res