Re: for problem...

From: Donal K. Fellows (donal.k.fellows_at_man.ac.uk)
Date: 12/02/03


Date: 2 Dec 2003 05:58:20 -0800

Donald Arseneau wrote:
> Removing the braces is not supposed to make a difference, thus there
> is TIP#132 proposing to change the default tcl_precision to 17.

Actually, IIRC #132 proposes altering the number formatting code so
that it generates the minimal representation of a floating-point
number in the normal case. (Unfortunately, it turns out that the
library we were going to rely on for doing this is a real mess, so the
work got a bit shelved...)

The original problem basically stems from the fact that Tcl (as with
most other computer programs) is representing 0.01 as a little bit
more than that value (it doesn't have a precise representation.)
Normally this doesn't matter, but it does when you decide to compare
the sum of a hundred of those values with 1.0. This is fundamental to
finite-precision floating-point numbers; you have similar problems
representing thirds in decimal, except there you tend to get totals
too small:

  1/3 = 0.333 (three decimal places for this example)
  Now 1/3+1/3+1/3 = 1
  so 0.333+0.333+0.333 ought to be 1 too, but it is really 0.999

So if you were doing a loop while you were less than one, you'd loop
one time more (i.e. with the value 1.332) which is surprising if
you're one of these people who ignores representation limitations...

Donal (this sort of thing has been in the computer literature since at
least the time I got my first computer, when it was already a
well-known phenomenon.)



Relevant Pages

  • Re: Is this expected behavior or not
    ... also talking about turning every slice reference into a loop. ... a reference to some statically known representation. ... The underlying model of a slice is an implicit loop. ...
    (comp.lang.ada)
  • Re: Is this expected behavior or not
    ... also talking about turning every slice reference into a loop. ... and a reference to some statically known representation. ... The underlying model of a slice is an implicit loop. ... Ada already has "loops" you ...
    (comp.lang.ada)
  • Re: Pythons simplicity philosophy
    ... operators is a simple labelled loop containing the items to be ... limited representation. ... Even with infix notation, there is a choice between making the default ... unnecessarily) instead of Ofor such overloadings, ...
    (comp.lang.python)
  • Re: for problem...
    ... DC>>So if you were doing a loop while you were less than one, ... DC>>one time more which is surprising if ... DC>>you're one of these people who ignores representation limitations... ... use *decimal* (actually BCD) floating point math. ...
    (comp.lang.tcl)
  • Re: Can this mostly useless while loop be optimized out?
    ... For strict portability, it appears that the while loop is mandatory, ... depending on whether a null pointer is represented as all-bits-zero. ... What are the actual implementations where a NULL has a representation ... Some compilers for microcontrollers etc. might have a reason to do ...
    (comp.lang.c)