summaryrefslogtreecommitdiffstats
path: root/ratize.scm
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2017-02-20 00:05:25 -0800
committerBryan Newbold <bnewbold@robocracy.org>2017-02-20 00:05:25 -0800
commit8ffbc2df0fde83082610149d24e594c1cd879f4a (patch)
treea2be9aad5101c5e450ad141d15c514bc9c2a2963 /ratize.scm
downloadslib-8ffbc2df0fde83082610149d24e594c1cd879f4a.tar.gz
slib-8ffbc2df0fde83082610149d24e594c1cd879f4a.zip
Import Upstream version 2a6upstream/2a6
Diffstat (limited to 'ratize.scm')
-rw-r--r--ratize.scm13
1 files changed, 13 insertions, 0 deletions
diff --git a/ratize.scm b/ratize.scm
new file mode 100644
index 0000000..d8cad11
--- /dev/null
+++ b/ratize.scm
@@ -0,0 +1,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)))