Re: Simple maths question
- From: Mark Janssen <mpc.janssen@xxxxxxxxx>
- Date: 10 May 2007 14:27:00 -0700
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
.
- Follow-Ups:
- Re: Simple maths question
- From: slebetman@xxxxxxxxx
- Re: Simple maths question
- References:
- Simple maths question
- From: Frem
- Simple maths question
- Prev by Date: Re: xy chart with trend line.
- Next by Date: Re: Wiki redux
- Previous by thread: Simple maths question
- Next by thread: Re: Simple maths question
- Index(es):
Relevant Pages
|