Re: Simple maths question



On 10 mei, 22:32, Frem <freminl...@xxxxxxxxx> wrote:
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.

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

.



Relevant Pages

  • Re: Will TCL release the variables automatically when the procedure is finished?
    ... it is NOT possible to "do too much float point math ... using Tcl": there are very large, ... If I were a betting man, I'd wager that namd is violating Tcl's threading principle: "an interp may only be called from the thread that created it". ... Also "As long as tcl doesn't do any floating point math in your script, this crash can't happen". ...
    (comp.lang.tcl)
  • Re: [SLE] Real Numbers representation in Tcl language
    ... But, Maura is talking floating point, and double precision floating point ... numbers in C are 64-bit on 32-bit and 64-bit platforms. ... Tcl has only one floating point storage format. ...
    (SuSE)
  • Re: variable value rounding...
    ... the reason that you can't avoid this is that floating ... If you really need this precision you can change to using ... I am new to tcl and can't solve this ...
    (comp.lang.tcl)
  • Re: newbie question: how to format "1 divided by 3" --> 0.33
    ... > of TCL) ... really need floating point math: ... Prev by Date: ...
    (comp.lang.tcl)
  • Re: calculate time span
    ... Floating point numbers (the double precision kind used by Tcl) have ... bigger than that but I am too lazy to look up the exact values;)). ...
    (comp.lang.tcl)