I've used this for a long time but it's scarcely tacit:
roundNums=: 3 : 0"1 0
NB.* roundNums: round numbers y to precision x, e.g.
NB. 0.1 roundNums 1.23 3.14159 2.718 -> 1.2 3.1 2.7.
NB. Optional 2nd left argument is single letter specifying
NB. type of rounding: Up, Down, or Banker's. Default
NB. banker's rounding (round halves up or down depending on
NB. parity of next (scaled) digit) tends to randomize bias.
1 roundNums y
:
RT=. 'B' NB. Default to Banker's rounding
TO=. x NB. Precision to which to round.
if. (2=#x)*.1=L. x do. 'TO RT'=. x end.
scaled=. y%TO NB. For Banker's: round down if last digit
even,
select. RT
case. 'B' do. RN=. 0.5*(0~:2|<.scaled)+.0.5~:1|scaled NB. up if odd.
case. 'D' do. RN=. (0.5=1|scaled){0 _0.5 NB. Round halves down.
case. 'U' do. RN=. 0.5 NB. Round halves up.
end.
TO*<.scaled+RN
)
On Tue, Jan 22, 2013 at 1:26 PM, Raul Miller <[email protected]> wrote:
> That's not something that can be resolved by a simple utility that
> handles floating point numbers. So if this is an issue you should
> implement something that fits your problem domain.
>
> --
> Raul
>
> On Tue, Jan 22, 2013 at 1:24 PM, Devon McCormick <[email protected]>
> wrote:
> > A bigger problem might be the slight upward bias in this algo. It's
> > exacerbated if you have a lot of halves in your data:
> >
> > rr=. -: 100?@$100
> > usus rr NB. usus -> usual stats: Min, max, mean, SD
> > 0 49.5 27.63 14.7935
> > usus round rr NB. Higher mean: significant?
> > 0 50 27.9 14.7802
> >
> > mean&> (round;]) -: 100?@$100
> > 22.28 21.975
> > -/mean&> (round;]) -: 100?@$100
> > 0.275
> > -/mean&> (round;]) -: 100?@$100
> > 0.225
> >
> > $100 ([: -: ?@$)&.>100$100
> > 100
> >
> > usus ([: -/ [: mean&> round ; ]) &> 100 ([: -: ?@$)&.>1e6$100
> > 0.13 0.365 0.250008 0.0249975
> > NB. Could be significant...
> >
> >
> > On Tue, Jan 22, 2013 at 12:08 PM, Raul Miller <[email protected]>
> wrote:
> >
> >> I don't think that that definition of round can be tacit and use under.
> >>
> >> A tacit implementation might be:
> >> 1&$: : ([ * <.@+~&1r2@%~)
> >>
> >> Note also: this is a verb, not an adverb. In this definition, 1 is the
> >> default value for x, not a control argument for :
> >>
> >> FYI,
> >>
> >> --
> >> Raul
> >>
> >> On Tue, Jan 22, 2013 at 11:55 AM, David Ward Lambert
> >> <[email protected]> wrote:
> >> > round =: 1&$: : (dyad def '<.@:(1r2+])&.:(%&x) y')
> >> > assert 1 -: round 1.2
> >> > assert 1.25 -: 0.25 round 1.2
> >> >
> >> > Round =: adverb def '<.@:(1r2+])&.:(%&m) y'
> >> >
> >> > Is there a tacit definition of round using under?
> >> > What is the tacit definition of adverb Round?
> >> > Thanks, Dave.
> >> >
> >> > ----------------------------------------------------------------------
> >> > For information about J forums see
> http://www.jsoftware.com/forums.htm
> >> ----------------------------------------------------------------------
> >> For information about J forums see http://www.jsoftware.com/forums.htm
> >>
> >
> >
> >
> > --
> > Devon McCormick, CFA
> > ^me^ at acm.
> > org is my
> > preferred e-mail
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm