home *** CD-ROM | disk | FTP | other *** search
- -- Produce the stream of closest rational approximations to a
- -- floating point number for a given size of denominator.
-
- fracts :: Float -> [Rational]
-
- type Op = ((Int,Int),(Int,Int)) -> ((Int,Int),(Int,Int)) in
- left, right, move, sternBrocRep
-
- left, right :: Op
- left = \ ((m,n),(m',n')) -> ((m,n),(m+m',n+n'))
- right = \ ((m,n),(m',n')) -> ((m+m',n+n'),(m',n'))
-
- move :: [Op] -> ((Int,Int),(Int,Int)) -> ((Int,Int),(Int,Int))
- move [] y = y
- move (x:xs) y = (move xs) (x y)
-
- sternBrocRep :: Float -> [Op]
- sternBrocRep x | x == 1.0 = []
- sternBrocRep x | x > 1.0 = right:(sternBrocRep (x-1.0))
- sternBrocRep x | x < 1.0 = left:(sternBrocRep (x/(1.0-x)))
-
- fracts x = [ z | sign = signum x,
- xs = sternBrocRep (abs x),
- k <- [1..],
- ((m,n),(m',n')) = move (take k xs) ((0,1),(1,0)),
- z = sign*((m+m'):/(n+n')),
- abs (x-z*1.0) < 0.5 ]
-