1 ! Copyright (C) 2010 John Benediktsson.
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: combinators kernel locals math math.functions ;
10 :: (simplest) ( n d n' d' -- val ) ! assumes 0 < n/d < n'/d'
12 n' d' /mod :> ( q' r' )
15 { [ q q' = not ] [ q 1 + ] }
17 d' r' d r (simplest) >fraction :> ( n'' d'' )
22 :: simplest ( x y -- val )
24 { [ x y > ] [ y x simplest ] }
26 { [ x 0 > ] [ x y [ >fraction ] bi@ (simplest) ] }
27 { [ y 0 < ] [ y x [ neg >fraction ] bi@ (simplest) neg ] }
31 : check-float ( x -- x )
32 dup float? [ "can't be floats" throw ] when ;
36 : approximate ( x epsilon -- y )
37 [ check-float ] bi@ [ - ] [ + ] 2bi simplest ;