1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-18 10:10:23 +02:00

Import SLIB 2d1.

This commit is contained in:
Keisuke Nishida 2001-04-14 11:24:45 +00:00
parent 92e7e03fae
commit 9ddacf866c
165 changed files with 61896 additions and 0 deletions

17
module/slib/ratize.scm Normal file
View file

@ -0,0 +1,17 @@
;;;; "ratize.scm" Find simplest number ratios
(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) (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)))