This is an interesting problem because it touches on a few subtle things.
% expr int(0.57 * 10000) 5699
This code (no wildcards, so it works "unsurprisingly") shows that floating point numbers themselves are awesome. In particular, 0.57 does not have an exact representation as an IEEE double-precision floating-point number (which is the base 2); in fact, this representation is slightly lower than exactly 0.57, and therefore when rounding (which is int(...) , and 10000 exactly), you drop to 5699. You will see the same behavior with other languages ββtoo.
% expr int([expr 0.57 * 10000]) 5700
Now it is especially interesting. First you see the internal calculation, and the resulting float is converted to a string (because there really is no other way to do this). Now you should use Tcl 8.4 (or earlier), where the default rendering rules were (in fact) what you got by typing the first 15 significant digits of the number; in this case, it gives you 5700.00000000000 (well, with some truncation of zeros to the right), and then is reinterpreted from scratch as the first double (exactly 5700.0) and then converted to 5700.
Number conversion rules changed in Tcl 8.5. Currently, when Tcl converts floating-point numbers to strings, it produces the shortest string that will convert back to exactly the same floating-point number (that is, sideways moving along the ground of the lines will give the same bit pattern as a result of double) . This in turn means that you never see the difference between the two things above. (If you really want to set a fixed number of decimal places, use format %.15g [expr 0.57 * 10000] .)
You will also not observe the behavior from 8.0 to 8.4 if you set up your expressions correctly, as the community has advised people to do this for more than ten years:
$ tclsh8.4 % expr {int([expr 0.57 * 10000])} 5699
This is because it does not cause the result of the internal call to expr interpreted as a string. (It is also faster and safer.)
source share