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:
parent
92e7e03fae
commit
9ddacf866c
165 changed files with 61896 additions and 0 deletions
17
module/slib/ratize.scm
Normal file
17
module/slib/ratize.scm
Normal 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)))
|
Loading…
Add table
Add a link
Reference in a new issue