D
Dr J R Stockton
In comp.lang.javascript message <[email protected]
To larger numbers. Numbers can be negative.
Well, one can if it has enough control parameters to select what is
required. The code for ordinary rounding, banker's rounding, and
truncation can be very similar, and the similar parts can be shared.
Not necessarily. Example : 100*(0.01 + 0.06) -> 6.999999999999999, so
Math.round(100*(0.01 + 0.06)) -> 7 unmakes a previous approximation.
That's why the purpose of a routine should be stated, as you yourself
say.
In particular, one should indicate whether it deals with negative
numbers (just those which should round to 0.00, or any?) and whether
Round(-X) = -Round(X) or Round(X+N) = N + Round(X) for X<0 and
'any' N.
In <URL:http://www.merlyn.demon.co.uk/js-round.htm#RAQ>, ApRnd does
ordinary rounding, banker's rounding, and truncation of approximately
accurate values, including choice of digits after the point and
characters before the point, in ten lines of code (using 2 library
functions each of 2 lines).
It's a good idea to read the newsgroup and its FAQ. See below.
It is illogical to say 0 is rounded either up or down - it isn't
rounded at all.
The rounding algorithm above results in a bias toward higher numbers.
To larger numbers. Numbers can be negative.
It is not possible to define a general rounding algorithm that will
suit all cases.
Well, one can if it has enough control parameters to select what is
required. The code for ordinary rounding, banker's rounding, and
truncation can be very similar, and the similar parts can be shared.
The act of rounding infers that an approximation is
about to be made and that some error (in the majority of cases) is
about to be introduced.
Not necessarily. Example : 100*(0.01 + 0.06) -> 6.999999999999999, so
Math.round(100*(0.01 + 0.06)) -> 7 unmakes a previous approximation.
Not knowing the use to which the number is to
be put infers ignorance of the consequences of errors introduced by
that approximation, hence the impossibility of defining how to deal
with it.
That's why the purpose of a routine should be stated, as you yourself
say.
In particular, one should indicate whether it deals with negative
numbers (just those which should round to 0.00, or any?) and whether
Round(-X) = -Round(X) or Round(X+N) = N + Round(X) for X<0 and
'any' N.
In <URL:http://www.merlyn.demon.co.uk/js-round.htm#RAQ>, ApRnd does
ordinary rounding, banker's rounding, and truncation of approximately
accurate values, including choice of digits after the point and
characters before the point, in ten lines of code (using 2 library
functions each of 2 lines).
It's a good idea to read the newsgroup and its FAQ. See below.