math.parser: simplify { integer ratio } >base.

db4
John Benediktsson 2015-07-19 16:45:46 -07:00
parent 89e3cc511e
commit 0dbc5e7d67
1 changed files with 11 additions and 16 deletions

View File

@ -420,24 +420,19 @@ GENERIC# >base 1 ( n radix -- str )
: >hex ( n -- str ) 16 >base ; inline : >hex ( n -- str ) 16 >base ; inline
M: integer >base M: integer >base
over 0 = [ {
2drop "0" { [ over 0 = ] [ 2drop "0" ] }
] [ { [ over 0 > ] [ positive>base ] }
over 0 > [ [ [ neg ] dip positive>base CHAR: - prefix ]
positive>base } cond ;
] [
[ neg ] dip positive>base CHAR: - prefix
] if
] if ;
M: ratio >base M: ratio >base
[ [ 0 < ] [ abs 1 /mod ] bi ] [ >fraction [ /mod ] keep ] [ [ >base ] curry tri@ ] bi*
[ [ positive>base ] curry ] bi* "/" glue over first-unsafe {
[ { CHAR: 0 [ nip ] }
[ [ numerator ] [ denominator ] bi ] dip bi@ "/" glue { CHAR: - [ append ] }
] keep rot [ drop ] [ [ drop "+" glue ]
swap call pick "-" "+" ? rot 3append } case ;
] if-zero swap [ CHAR: - prefix ] when ;
<PRIVATE <PRIVATE