Skip to content
Tcl Source Code View Ticket
Login

View Ticket

expr return value

Closed / Invalid2828631fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 17 years ago
Descriptionplain2009-07-28 21:15:15
In Tcl 8.5.4:

% expr 0.8 - 0.35
0.450000000000007
%expr 0.6 - 0.15
0.449999999999996

In Tcl 8.4:

% expr 0.8 - 0.35
0.45
%expr 0.6 - 0.15
0.45

OS is Linux.

So scripts that used to work in 8.4 fail in 8.5.4 for our customers.

Is there a fix for this issue? workaround?

Thanks,

Israel Taller
dkfplain2009-07-29 04:49:34
The issue is that 8.5 uses different rounding by default when converting doubles to strings, not that 8.5 has suddenly become less accurate (the deep problem is that 0.35, 0.45 and 0.15 do not have finite expansions in IEEE binary arithmetic). Compare these two logs and notice that the values are actually the same except for the different number of decimal places.

% info patch
8.4.7
% expr 0.8 - 0.35
0.45
% expr 0.6 - 0.15
0.45
% expr 0.8 - 0.35 - 0.45
5.55111512313e-17
% expr 0.6 - 0.15 - 0.45
-5.55111512313e-17

% info patch
8.5.2
% expr 0.8 - 0.35 - 0.45
5.551115123125783e-17
% expr 0.6 - 0.15 - 0.45
-5.551115123125783e-17

I'll let dgp suggest a workaround for you.
dgpplain2009-07-29 19:30:19
allow_comments - 1


The usual reply is why do you care?
The 8.5 formats are actually more accurate,
so if you are doing calculations with the
values they should be better - a bug fix.

If the issue is something pretty for display,
that's what [format] is for.

More from the release notes of Tcl 8.5.0,
http://sf.net/project/shownotes.php?group_id=10894&release_id=562516 :

" * When floating point values are converted to a string representation,
   the conversion is no longer governed by a default value of 12 for
   $::tcl_precision.  The default value is now 0, meaning the shortest
   string representation that when parsed produces precisely the same
   floating point value.  Some scripts that display floating point values
   may begin to see displays like "1.9999999999999998" where "2.0" had
   been displayed before.  Revise the script to explicitly [format]
   values as desired.  As a last resort, [set ::tcl_precision 12] will
   restore compatibility within the current thread, but be aware this
   will also resurrect bugs that the change has fixed."
Log in (anonymous is fine) to add a comment.