Re: "round" - improvement suggestion



Kevin Kenny wrote:
Eric Hassold wrote:
Also, rounding is not strictly equivalent to formatting. format returns a string, round(value,n) (with n>0) returns a float (an integer or bignum if n==0). So I believe OP sample is incorrect, since
set a [round 34.258123 2]
which would be equivalent to
set a [expr {round(34.258123,2)}]
should return a float number for round(34.258123*100.0)/100.0, which, due to IEEE floating point representation, may be different from 34.26. For example, in Python:

>>> round(34.258123,2)
34.259999999999998

Python isn't getting this quite right. Tcl 8.5 does a good bit better,
by choosing the shortest string of digits that reconverts to the
original floating point number:

% proc round2 {x} {expr {0.01 * round(100.0 * $x)}}
% round2 34.258123
34.26
% info tclversion
8.5


Hello,

Yes, thanks to the tcl_precision used in getting string representation of floats.

% proc round2 {x} {expr {0.01 * round(100.0 * $x)}}
% round2 34.258123
34.26
% format %.2f 34.258123
34.26

% set tcl_precision 17
17
% round2 34.258123
34.259999999999998
% format %.2f 34.258123
34.26

My sample aims only to show difference between round and format. Former returns a float object, which append to be correcly displayed according to tcl_precision setting, latter returns always a string, which won't change, but which will be unefficient if chaining returned value in other math ops.

Another difference between format and round:

% round2 1.0000003
1.0
% format %.2f 1.0000003
1.00

Eric
.


Quantcast