continued-fraction 0.1.0.0 → 0.1.0.3
raw patch · 6 files changed
+48/−14 lines, 6 filesdep +freePVP ok
version bump matches the API change (PVP)
Dependencies added: free
API changes (from Hackage documentation)
Files
- Justfile +13/−0
- bench/Bench.hs +2/−1
- cabal.project.local +4/−0
- continued-fraction.cabal +14/−6
- src/Num/ContinuedFraction.hs +14/−6
- stack.yaml +1/−1
+ Justfile view
@@ -0,0 +1,13 @@+test: build+ cabal new-test++build:+ cabal new-build++bench: build+ cabal new-bench+ $(fd 'fractions-bench$' | tail -n1)++docs:+ cabal new-haddock+ firefox-trunk $(fd '^index.html')
bench/Bench.hs view
@@ -6,5 +6,6 @@ main :: IO () main = defaultMain [ bgroup "under"- [ bench "pi" $ nf (approximate (pi :: Double)) (100 :: Integer) ]+ [ bench "pi" $ nf (approximate (pi :: Double)) (100 :: Integer)+ , bench "sqrt 3" $ nf (approximate (sqrt (3 :: Double) :: Double)) (1000000 :: Integer) ] ]
+ cabal.project.local view
@@ -0,0 +1,4 @@+documentation: True+haddock-hoogle: True+haddock-internal: True+optimization: 2
continued-fraction.cabal view
@@ -1,10 +1,10 @@ name: continued-fraction-version: 0.1.0.0+version: 0.1.0.3 synopsis: Types and functions for working with continued fractions in Haskell description: This package provides facilities for working with both continued fractions and rational approximants. It uses lists internally, so it will probably not be fast if you need large convergents.-homepage: https://github.com/vmchale/continued-fractions#readme+homepage: https://hub.darcs.net/vmchale/continued-fraction#readme license: BSD3 license-file: LICENSE author: Vanessa McHale@@ -15,6 +15,8 @@ extra-source-files: README.md , stack.yaml , .travis.yml+ , Justfile+ , cabal.project.local cabal-version: >=1.10 Flag development {@@ -26,8 +28,9 @@ library hs-source-dirs: src exposed-modules: Num.ContinuedFraction- build-depends: base >= 4.7 && < 5+ build-depends: base >= 4.9 && < 5 , recursion-schemes >= 5.0+ , free default-language: Haskell2010 if flag(development) ghc-options: -Werror@@ -40,6 +43,8 @@ build-depends: base , continued-fraction , hspec+ if flag(development)+ ghc-options: -Werror ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat default-language: Haskell2010 @@ -50,9 +55,12 @@ build-depends: base , continued-fraction , criterion- ghc-options: -O2 -Wall -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+ if flag(development)+ ghc-options: -Werror+ ghc-options: -Wall -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+ -- -O2 default-language: Haskell2010 source-repository head- type: git- location: https://github.com/vmchale/continued-fractions+ type: darcs+ location: https://hub.darcs.net/vmchale/continued-fraction
src/Num/ContinuedFraction.hs view
@@ -7,8 +7,7 @@ , convergent ) where -import Control.Applicative (liftA2)-import Data.Functor.Foldable (ListF (..), ana)+import Data.Functor.Foldable (ListF (..), apo) import Data.List.NonEmpty (NonEmpty (..), fromList) import Data.Maybe (fromJust) import Data.Ratio (Ratio, denominator, (%))@@ -30,11 +29,12 @@ -- >>> continuedFraction 2 -- [2] continuedFraction :: (RealFrac a, Integral b) => a -> [b]-continuedFraction = liftA2 (:) floor (ana coalgebra)+continuedFraction = apo coalgebra where coalgebra x- | isInteger x = Nil- | otherwise = Cons (floor alpha) alpha- where alpha = 1 / (x - realToFrac (floor x :: Int))+ | isInteger x = go $ Left []+ | otherwise = go $ Right alpha+ where alpha = 1 / (x - realToFrac (floor x :: Integer))+ go = Cons (floor x) -- | This takes a list of integers and returns the corresponding rational number, returning "Nothing" on empty lists. --@@ -47,6 +47,13 @@ collapseFractionM [x] = Just $ fromIntegral x % 1 collapseFractionM (x:xs) = fmap ((fromIntegral x % 1 +) . (1 /)) (collapseFractionM xs) +{-collapseFractionH :: (Integral a) => [Integer] -> (Ratio a)+collapseFractionH = histo algebra+ where+ algebra Nil = 1 % 1+ algebra (Cons x (_:<Nil)) = fromIntegral x % 1+ algebra (Cons x (_:<Cons _ (x':<_))) = ((fromIntegral x) % 1) * (numerator x' % denominator x')-}+ -- | Take a non-empty list of integers and return the corresponding rational number. -- -- >>> collapseFraction (1 :| [2,2,2])@@ -62,6 +69,7 @@ convergent :: (RealFrac a, Integral b) => Int -> a -> Ratio b convergent n x = fromJust . collapseFractionM $ take n (continuedFraction x) +-- FIXME this should be intelligent enough to do some sort of caching. -- | Find the best rational approximation to a number such that the denominator is bounded by a given value. -- -- >>> approximate pi 100
stack.yaml view
@@ -1,4 +1,4 @@-resolver: lts-9.3+resolver: lts-9.10 packages: - '.' extra-deps: