diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:28 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:28 -0800 |
commit | 87b82b5822ca54228cfa6df29be3ad9d4bc47d16 (patch) | |
tree | 1eb4f87abd38bea56e08335d939e8171d5e7bfc7 /ratize.scm | |
parent | bd9733926076885e3417b74de76e4c9c7bc56254 (diff) | |
download | slib-87b82b5822ca54228cfa6df29be3ad9d4bc47d16.tar.gz slib-87b82b5822ca54228cfa6df29be3ad9d4bc47d16.zip |
Import Upstream version 2d2upstream/2d2
Diffstat (limited to 'ratize.scm')
-rw-r--r-- | ratize.scm | 26 |
1 files changed, 15 insertions, 11 deletions
@@ -1,13 +1,17 @@ -;;;; "ratize.scm" Convert number to rational number +;;;; "ratize.scm" Find simplest number ratios -(define (rational:simplest x y) - (define (sr x y) (let ((fx (floor x)) (fy (floor y))) - (cond ((not (< fx x)) fx) - ((= fx fy) (+ fx (/ (sr (/ (- y fy)) (/ (- x fx)))))) - (else (+ 1 fx))))) - (cond ((< y x) (rational:simplest y x)) - ((not (< x y)) (if (rational? x) x (slib:error))) +(define (find-ratio-between x y) + (define (sr x y) + (let ((fx (inexact->exact (floor x))) (fy (inexact->exact (floor y)))) + (cond ((>= fx x) (list fx 1)) + ((= fx fy) (let ((rat (sr (/ (- y fy)) (/ (- x fx))))) + (list (+ (cadr rat) (* fx (car rat))) (car rat)))) + (else (list (+ 1 fx) 1))))) + (cond ((< y x) (find-ratio-between y x)) + ((>= x y) (list x 1)) ((positive? x) (sr x y)) - ((negative? y) (- (sr (- y) (- x)))) - (else (if (and (exact? x) (exact? y)) 0 0.0)))) -(define (rationalize x e) (rational:simplest (- x e) (+ x e))) + ((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))) |