File: ratize.scm

package info (click to toggle)
slib 2c7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,216 kB
  • ctags: 1,613
  • sloc: lisp: 19,483; makefile: 334; sh: 144
file content (13 lines) | stat: -rw-r--r-- 525 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
;;;; "ratize.scm" Convert number to rational number

(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)))
	((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)))