User:Andrew Nikitin/ffrac

From J Wiki
Jump to navigation Jump to search
NB. format as fraction on one line
NB. x=denominator
ffrac=:4 : 0">
assert. (x>0) *. x=<.x
r=.'-' #~ y<0
a=.<.0.5+x*y=.|y
'n d'=.(% +./) a,x
i=.<.n%d
n=.d|n
r=.r,(i>0)#":i
r=.r,(n>0) # (' '#~i>0),(":n),'/',":d
r=.r,('+'#~a<x*y),('-'#~a>x*y)
)