Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

TCL(Tool Command Language) Scripting

Simple maths question


Hello,

I was demonstrating how easy Tcl is to use (or at least to learn to use)
when I was immediately flummoxed.

On a Windows XP machine I created a trivial proc:

(bin) 289 % proc subtract {a b} {
  return [expr {$a - $b}]

}

(bin) 290 % subtract 3 2.2
0.7999999999999998

Eh? This was using Tcl 8.5a4. It works properly in 8.4.11. Is this a
known bug (sorry if it is)?

Thanks,
Frem.

On 10 mei, 22:32, Frem <freminl@gmail.com> wrote:

It is working properly in 8.5 as well. The difference between 8.4 and
8.5 is that the tcl_precision global is set differently by default.
In 8.5 the two lines:

set tcl_precision 12
subtract 3 2.2

will give the same result as in 8.4.

The real reason you get this result however is that floating point
math is not precise on a computer (0.2 is a repeating binary
fraction).
This is not a limitation of Tcl, but a limitation of doing decimal
floating point math in binary. See http://wiki.tcl.tk/879 for a more
elaborate explanation. Also see http://www.python.org/doc/current/tut/node16.html
for another explanation which also shows this is a general problem
with floating point math not limited to Tcl

Mark

On May 11, 5:27 am, Mark Janssen <mpc.jans@gmail.com> wrote:

People should be reminded with floating point, any fraction that
doesn't end in a '5' is imprecise. Just like with regular pen-on-paper
decimal math, 1/3 is imprecise (interestingly if you do your math in
base 3, 1/3 is not a repeating fraction and can be precisely
represented).
For perfect precision with rational numbers, you can treat them as
pairs of integer {numerator denominator}. See "Playing with
rationals", http://wiki.tcl.tk/13877
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc