This site is a static rendering of the Trac instance that was used by R7RS-WG1 for its work on R7RS-small (PDF), which was ratified in 2013. For more information, see Home.
Source for wiki RationalizeDefinition version 1
author
cowan
comment
ipnr
69.195.56.27
name
RationalizeDefinition
readonly
0
text
This implementation of `rationalize` is taken from the IEEE Scheme standard, which is not freely available. The code is by Alan Bawden, and the theory is from Hardy and Wright's ''Introduction to the Theory of Numbers'', 5th edition (1979).
{{{
(define (rationalize x e)
(simplest-rational (- x e) (+ x e)))
(define (simplest-rational x y)
(define (simplest-rational-internal x y)
;; assumes 0 < X < Y
(let ((fx (floor x))
(fy (floor y)))
(cond ((not (< fx x))
fx)
((= fx fy)
(+ fx
(/ (simplest-rational-internal
(/ (- y fy))
(/ (- x fx))))))
(else
(+ 1 fx)))))
;; do some juggling to satisfy preconditions
;; of simplest-rational-internal.
(cond ((< y x)
(simplest-rational y x))
((not (< x y))
(if (rational? x) x (error)))
((positive? x)
(simplest-rational-internal x y))
((negative? y)
(- (simplest-rational-internal (- y)
(- x))))
(else
(if (and (exact? x) (exact? y))
0
0.0))))
}}}
time
2012-04-19 08:39:12
version
1