Primitive ratnum operations implemented.
[scheme.forth.jl.git] / src / integer.4th
1 \ Integer arithmetic words
2
3 : sort-pair
4     2dup > if
5         swap
6     then
7 ;
8
9 ( Find the GCD of n1 and n2 where n2 < n1. )
10 : gcd ( n1 n2 -- m )
11     sort-pair
12     over 0= if
13         swap drop
14     else
15         over mod
16         recurse
17     then
18 ;
19
20 : simplify ( n d -- n' d' )
21     swap dup 0< -rot abs swap
22     2dup gcd 
23     swap over ( b n1 c n2 c )
24     / ( b n1 c n2' )
25     -rot / ( b n2' n1' )
26
27     rot if
28         negate
29     then
30
31     swap
32 ;