#4835 Bad result in expr command

obsolete: 8.6b1
closed-invalid
5
2011-06-06
2011-06-06
vadimosk
No

OS Ubuntu Linux 10.04 LTS (original repository)

tclsh8.4 - OK
%expr 9.9 + 9.9 + 9.9
29.7
%expr 9.9 * 3
29.7

tclsh8.5 - BAD
%expr 9.9 + 9.9 + 9.9
29.700000000000003
%expr 9.9 * 3
29.700000000000003

tclsh8.6 - BAD (package from Debian unstable - tcl8.6_8.6.0~b1-4_amd64)
%expr 9.9 + 9.9 + 9.9
29.700000000000003
%expr 9.9 * 3
29.700000000000003

Discussion

  • Donal K. Fellows

    • status: open --> closed-invalid
     
  • Donal K. Fellows

    9.9 is not representable in a finite number of bits in binary floating point (such as IEEE double precision floats, which Tcl uses under the hood because they've got proper hardware support on virtually all modern hardware) so an approximation is used. That approximation is very slightly larger than the "true value" and causes the difference you see; the "* 3" just makes the error three times larger (though proportionately about the same).

    The difference between 8.4 and 8.5 is that 8.5 uses a different floating point number renderer that normally/by-default avoids the worst of the problem (i.e., it uses the shortest decimal representation that will provide the boolean value that we've got internally). Unfortunately, the multiply by 3 has grown the error to the point where it can no longer be hidden this way. Oh well, that's life.

    The recommended workaround is to use [format "%.15g" $value] when producing a value for _display_ to users. The rest of the time, perfect fidelity of value (which you get with the current settings, unlike before) is actually more useful. Really.

     
  • Donal K. Fellows

    tl;dr: We know. It's deliberate. Use [format] to work around.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks