Skip to content
Tcl Source Code View Ticket
Login

View Ticket

wrong expr result

Closed / Invalid1152828fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 22 years ago
Descriptionplain2005-02-27 10:37:52
the following steps yield a wrong expr result:
% expr (2.13 - 1.97) - (2.115 - 1.955)
-2.22044604925e-16

expected results is 0.0.

Error occures for also for different numbers

Running on tclsh8.3 and tclsh8.4.9 (latest)

emails: [email protected]
            [email protected]


Thanks
dkfplain2005-03-03 05:18:43
Logged In: YES 
user_id=79902

Floating-point numbers are not generally great at
representing arbitrary decimal numbers, so the differences
in a case like this can be signficant. Witness, where we're
using [format] to force a more detailed display of the
actual values involved.:
  % format %.17f [expr 2.115-1.955]
  0.16000000000000014
  % format %.17f [expr 2.13-1.97]
  0.15999999999999992
dkf Closed ticket [1152828fff]. [History diff]2005-03-03 05:23:33
dkfplain2005-03-03 05:23:34
Logged In: YES 
user_id=79902

BTW, you'd hit the same problem in any other language with
IEEE floating point, and an analog in virtually any other
modern programming system too. Not A Bug, just a
(Mis)Feature of floats.
Log in (anonymous is fine) to add a comment.