fixed-point 0.1.0.1 → 0.2.0.0
raw patch · 2 files changed
+59/−9 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.Fixed.Binary: instance (HasResolution r, Bits a, Real a) => Num (Fixed r a)
- Data.Fixed.Binary: instance (HasResolution r, Bits a, Real a) => Real (Fixed r a)
- Data.Fixed.Binary: instance (HasResolution r, Bits a, Real a) => Show (Fixed r a)
+ Data.Fixed.Binary: instance (HasResolution r, Bits a, Integral a) => Num (Fixed r a)
+ Data.Fixed.Binary: instance (HasResolution r, Bits a, Integral a) => Real (Fixed r a)
+ Data.Fixed.Binary: instance (HasResolution r, Bits a, Integral a) => Show (Fixed r a)
+ Data.Fixed.Binary: type E10 = S (S E8)
+ Data.Fixed.Binary: type E20 = E10 :+ E10
+ Data.Fixed.Binary: type E30 = E20 :+ E10
Files
- Data/Fixed/Binary.hs +52/−7
- fixed-point.cabal +7/−2
Data/Fixed/Binary.hs view
@@ -19,20 +19,26 @@ , divMod' , Fixed () , HasResolution (..)- , E0, E1, E2, E4, E8, E16, E32, E64+ , E0, E1, E2, E4, E8, E10, E16, E20, E30, E32, E64 , S, P , fixedRadix, fixedSize , (:+), (*.) , (:-), (/.) ) where +import Control.Applicative+import Control.Arrow+import Control.Monad import Data.Bits import Data.Fixed (div', mod', divMod')+import Data.List import Data.Int+import Data.Maybe import Data.Ratio import Data.Typeable import Data.Word import Text.Read+import qualified Text.Read.Lex as L -- | The first type parameter represents the number of bits to devote -- to the fractional part of the number. The second type parameter is@@ -63,13 +69,36 @@ where withType :: (Fixed r a -> Fixed r a) -> Fixed r a withType g = g undefined +-- Read a signed number. Stolen from GHC.Read.+readNumber :: Num a => (L.Lexeme -> ReadPrec a) -> ReadPrec a+readNumber convert =+ parens+ ( do x <- lexP+ case x of+ L.Symbol "-" -> do y <- lexP+ n <- convert y+ return (negate n)++ _ -> convert x+ )++-- Stolden from GHC.Read.+convertFrac :: Fractional a => L.Lexeme -> ReadPrec a+convertFrac (L.Int i) = return (fromInteger i)+convertFrac (L.Rat r) = return (fromRational r)+convertFrac _ = pfail+ instance (HasResolution r, Bits a, Integral a) => Read (Fixed r a) where- readPrec = fmap fromRational readPrec+ readPrec = readNumber convertFrac -instance (HasResolution r, Bits a, Real a) => Show (Fixed r a) where- show = show . toRational+instance (HasResolution r, Bits a, Integral a) => Show (Fixed r a) where+ show = reverse . uncurry (++) . second ('.':) .+ join (***) (\str -> if null str then "0" else str) . uncurry splitAt .+ second (reverse . show . numerator) . fromJust .+ find ((==1) . denominator . snd) . iterate (succ *** (*10)) . (,) 0 .+ toRational -instance (HasResolution r, Bits a, Real a) => Num (Fixed r a) where+instance (HasResolution r, Bits a, Integral a) => Num (Fixed r a) where {-# INLINABLE (+) #-} (+) = inFixed2 (+) {-# INLINABLE (-) #-}@@ -85,9 +114,9 @@ {-# INLINABLE fromInteger #-} fromInteger i = withResolution $ Fixed . shiftL (fromInteger i) -instance (HasResolution r, Bits a, Real a) => Real (Fixed r a) where+instance (HasResolution r, Bits a, Integral a) => Real (Fixed r a) where {-# INLINABLE toRational #-}- toRational x = toRational (unFixed x) / toRational (2 ^ resolution x :: Int)+ toRational x = toRational (unFixed x) / toRational (2 ^ resolution x :: Integer) instance (HasResolution r, Bits a, Integral a) => Fractional (Fixed r a) where {-# INLINABLE (/) #-}@@ -152,6 +181,19 @@ {-# INLINABLE (/.) #-} (/.) = inFixed2 div +-- TODO Don't assume it's a binary float so that we can actually+-- expose this function.+toRealFloat :: (HasResolution r, Integral a, RealFloat b) => Fixed r a -> b+{-# INLINABLE toRealFloat #-}+toRealFloat = liftA2 encodeFloat (fromIntegral . unFixed) (negate . resolution)+{-# SPECIALIZE toRealFloat :: (HasResolution r, Integral a) => Fixed r a -> Float #-}+{-# SPECIALIZE toRealFloat :: (HasResolution r, Integral a) => Fixed r a -> Double #-}++{-# RULES+"realToFrac/Float" forall (x :: (HasResolution r, Integral a) => Fixed r a). realToFrac x = toRealFloat x :: Float+"realToFrac/Double" forall (x :: (HasResolution r, Integral a) => Fixed r a). realToFrac x = toRealFloat x :: Double+ #-}+ data E0 -- | Increment a resolution@@ -175,7 +217,10 @@ type E2 = E1 :+ E1 type E4 = E2 :+ E2 type E8 = E4 :+ E4+type E10 = S (S E8) type E16 = E8 :+ E8+type E20 = E10 :+ E10+type E30 = E20 :+ E10 type E32 = E16 :+ E16 type E64 = E32 :+ E32
fixed-point.cabal view
@@ -1,5 +1,5 @@ Name: fixed-point-Version: 0.1.0.1+Version: 0.2.0.0 Synopsis: Binary fixed-point arithmetic Description: This package defines a type for binary fixed-precision arithmetic. Its interface is@@ -18,6 +18,11 @@ Library Build-depends: base == 4.3.* Exposed-modules: Data.Fixed.Binary+ Extensions: DeriveDataTypeable,+ EmptyDataDecls,+ GeneralizedNewtypeDeriving,+ TypeFamilies,+ TypeOperators GHC-options: -Wall -fwarn-tabs -funfolding-use-threshold=16 Source-repository head@@ -27,4 +32,4 @@ source-repository this type: darcs location: http://patch-tag.com/r/jmcarthur/fixed-point- tag: v0.1.0.0+ tag: v0.2.0.0