Primitive ratnum operations implemented.
[scheme.forth.jl.git] / src / integer.4th
diff --git a/src/integer.4th b/src/integer.4th
new file mode 100644 (file)
index 0000000..3ecabfc
--- /dev/null
@@ -0,0 +1,32 @@
+\ Integer arithmetic words
+
+: sort-pair
+    2dup > if
+        swap
+    then
+;
+
+( Find the GCD of n1 and n2 where n2 < n1. )
+: gcd ( n1 n2 -- m )
+    sort-pair
+    over 0= if
+        swap drop
+    else
+        over mod
+        recurse
+    then
+;
+
+: simplify ( n d -- n' d' )
+    swap dup 0< -rot abs swap
+    2dup gcd 
+    swap over ( b n1 c n2 c )
+    / ( b n1 c n2' )
+    -rot / ( b n2' n1' )
+
+    rot if
+        negate
+    then
+
+    swap
+;