dfrac (empty) → 0.1.1.0
raw patch · 4 files changed
+102/−0 lines, 4 filesdep +basedep +scientificsetup-changed
Dependencies added: base, scientific
Files
- Data/DFrac.hs +56/−0
- LICENSE +20/−0
- Setup.hs +2/−0
- dfrac.cabal +24/−0
+ Data/DFrac.hs view
@@ -0,0 +1,56 @@+module Data.DFrac (DFrac(..), setPrec) where+import GHC.Read+import Data.Ratio+import Data.Scientific++data DFrac = DFrac {+ places :: Int,+ number :: Rational+ }++instance Eq DFrac where+ x == y = number x == number y+instance Ord DFrac where+ compare x y = compare (number x) (number y)++instance Num DFrac where+ x + y = dfrac min x y $ number x + number y+ x - y = dfrac min x y $ number x - number y+ x * y = dfrac min x y $ number x * number y+ abs x = DFrac (places x) $ abs $ number x+ signum x = DFrac (places x) $ signum $ number x+ fromInteger = DFrac 16 . fromInteger+instance Fractional DFrac where+ x / y = dfrac min x y $ number x / number y+ fromRational = DFrac 16 . fromRational+instance Real DFrac where+ toRational = number+instance RealFrac DFrac where+ properFraction x = (a, DFrac (places x) b)+ where+ (a, b) = properFraction $ number x+ +instance Read DFrac where+ readsPrec p z = map (\(d, s) -> (DFrac 16 $ toRational d, s)) r+ where+ r :: [(Scientific, String)]+ r = readsPrec p z+ readListPrec = readListPrecDefault+ readList = readListDefault+instance Show DFrac where+ show (DFrac p r) = show i ++ "." ++ tail (show f)+ where+ n = numerator r+ d = denominator r+ i = n `div` d+ f :: Integer+ f = truncate $ (10^p) * (1+(snd $ properFraction r))++setPrec :: Int -> DFrac -> DFrac+setPrec p r = r { places = p }++dfrac :: (Int -> Int -> Int) -> DFrac -> DFrac -> Rational -> DFrac+dfrac f x y z = DFrac (prec f x y) z++prec :: (Int -> Int -> Int) -> DFrac -> DFrac -> Int+prec f x y = f (places x) (places y)
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 George Rogers++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ dfrac.cabal view
@@ -0,0 +1,24 @@+-- Initial dfrac.cabal generated by cabal init. For further documentation,+-- see http://haskell.org/cabal/users-guide/++name: dfrac+version: 0.1.1.0+synopsis: A package for precise decimal arithmatic using rationals.+description: A package for precise decimal arithmatic using rationals.+license: MIT+license-file: LICENSE+author: George Rogers+maintainer: grogers385@gmail.com+-- copyright: +category: Math+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: Data.DFrac+ -- other-modules: + -- other-extensions: + build-depends: base >=4 && <5, scientific >=0.3 && <0.4+ -- hs-source-dirs: + default-language: Haskell2010