Paste: approxRational
Author: | mrjbq7 |
Mode: | factor |
Date: | Tue, 7 Sep 2010 06:10:45 |
Plain Text |
USING: combinators kernel locals math math.functions ;
:: (simplest)
n d /mod :>
n' d' /mod :>
{
{ [ r zero? ] [ q ] }
{ [ q q' = not ] [ q 1 + ] }
[
d' r' d r (simplest) >fraction :>
q n'' * d'' + n'' /
]
} cond ;
:: simplest
{
{ [ x y > ] [ y x simplest ] }
{ [ x y = ] [ x ] }
{ [ x 0 > ] [ x y [ >fraction ] bi@ (simplest) ] }
{ [ y 0 < ] [ y x [ neg >fraction ] bi@ (simplest) neg ] }
[ 0 ]
} cond ;
: approximate
[ - ] [ + ] 2bi simplest ;
New Annotation