continued-fraction-0.1.0.8: Types and functions for working with continued fractions in Haskell

Safe HaskellSafe
LanguageHaskell2010

Num.ContinuedFraction

Contents

Synopsis

Conversion between numbers and continued fractions

continuedFraction :: (RealFrac a, Integral b) => a -> [b] Source #

This take a number and returns its continued fraction expansion as a list of Integers.

>>> continuedFraction 2
[2]

collapseFraction :: (Integral a, Integral b) => NonEmpty b -> Ratio a Source #

Take a non-empty list of integers and return the corresponding rational number.

>>> collapseFraction (1 :| [2,2,2])
17 % 12

Rational approximations using continued fractions

approximate :: (RealFrac a, Integral b) => a -> b -> Ratio b Source #

Find the best rational approximation to a number such that the denominator is bounded by a given value.

>>> approximate pi 100
22 % 7

convergent :: (RealFrac a, Integral b) => Int -> a -> Ratio b Source #

Find a given convergent of the continued fraction expansion of a number

>>> convergent 4 $ sqrt 2
17 % 12

Pretty-printer for continued fractions

prettyFracM :: Show a => [a] -> Maybe String Source #

Print a list as a continued fraction.

>>> prettyFracM (take 5 $ continuedFraction (sqrt 2))
"[1; 2, 2, 2, 2]"