diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:29 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:29 -0800 |
commit | 8466d8cfa486fb30d1755c4261b781135083787b (patch) | |
tree | c8c12c67246f543c3cc4f64d1c07e003cb1d45ae /ratize.scm | |
parent | 87b82b5822ca54228cfa6df29be3ad9d4bc47d16 (diff) | |
download | slib-8466d8cfa486fb30d1755c4261b781135083787b.tar.gz slib-8466d8cfa486fb30d1755c4261b781135083787b.zip |
Import Upstream version 3a1upstream/3a1
Diffstat (limited to 'ratize.scm')
-rw-r--r-- | ratize.scm | 43 |
1 files changed, 41 insertions, 2 deletions
@@ -1,5 +1,46 @@ ;;;; "ratize.scm" Find simplest number ratios +;;@code{(require 'rationalize)} +;;@ftindex rationalize + +;;The procedure @dfn{rationalize} is interesting because most programming +;;languages do not provide anything analogous to it. Thanks to Alan +;;Bawden for contributing this algorithm. + +;;@body +;;Computes the correct result for exact arguments (provided the +;;implementation supports exact rational numbers of unlimited precision); +;;and produces a reasonable answer for inexact arguments when inexact +;;arithmetic is implemented using floating-point. +;; +(define (rationalize x e) (apply / (find-ratio x e))) + +;;@code{Rationalize} has limited use in implementations lacking exact +;;(non-integer) rational numbers. The following procedures return a list +;;of the numerator and denominator. + +;;@body +;;@0 returns the list of the @emph{simplest} +;;numerator and denominator whose quotient differs from @1 by no more +;;than @2. +;; +;;@format +;;@t{(find-ratio 3/97 .0001) @result{} (3 97) +;;(find-ratio 3/97 .001) @result{} (1 32) +;;} +;;@end format +(define (find-ratio x e) (find-ratio-between (- x e) (+ x e))) + + +;;@body +;;@0 returns the list of the @emph{simplest} +;;numerator and denominator between @1 and @2. +;; +;;@format +;;@t{(find-ratio-between 2/7 3/5) @result{} (1 2) +;;(find-ratio-between -3/5 -2/7) @result{} (-1 2) +;;} +;;@end format (define (find-ratio-between x y) (define (sr x y) (let ((fx (inexact->exact (floor x))) (fy (inexact->exact (floor y)))) @@ -13,5 +54,3 @@ ((negative? y) (let ((rat (sr (- y) (- x)))) (list (- (car rat)) (cadr rat)))) (else '(0 1)))) -(define (find-ratio x e) (find-ratio-between (- x e) (+ x e))) -(define (rationalize x e) (apply / (find-ratio x e))) |