Created
September 25, 2012 03:39
-
-
Save shirok/3779852 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (rationalize1 x e) | |
(define (refine lo hi) | |
(let* ([M (/ (+ (numerator lo) (numerator hi)) | |
(+ (denominator lo) (denominator hi)))] | |
[delta (abs (- x M))]) | |
(cond | |
[(<= delta e) M] ;; found | |
[(< M x) (refine M hi)] | |
[else (refine lo M)]))) | |
(if (< (abs (- x (round x))) e) | |
(round->exact x) | |
(refine (floor->exact x) (ceiling->exact x)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment