Version 10 of Pure numbers

Updated 2004-05-23 13:01:46

FW: A "pure number" (compare pure list) is a number with no string representation in memory (though of course it can be manipulated as a string - and as soon as it is, it gains a string form).

Any integer value computed by the Tcl interpreter rather than provided by the user will be in pure integer form. The easy way to attain this is taking a numeric value (let's call it x) and performing an incr x 0, which will cause Tcl to compute the value of $x plus zero (which is hopefully just $x), and since the result is a number computed by Tcl, there's no need for a string form.

The situation where this is needed is arcane indeed, though - since invoking expr to perform any calculation on a value will also do this (as long as you do actually perform some operation - if it's just expr {$x} that wouldn't cause Tcl to calculate anything, so it would just return the original value of $x intact). Presumably every time you create an integer you're going to be performing some calculation on it, so the times when you'll need to use the incr technique are few and far between.

The only time I could think of when doing this would be productive is when one is reading a large number of large numbers from a file into a list or such, without necessarily changing their value, and doesn't want to take up much memory.

Donal Fellows (whose explanations were mighty helpful in the creation of this page) points out, however, that if you take into account Tcl_Obj overhead, the most a string representation could add - for an integer, at least - is a 2x gain in memory profile.

DKF - Note that there's not much point in worrying whether a number is pure in Tcl or not. Just use the things. Where you can gain is when you are storing loads of instances of the same value (e.g. a list of zeros, ones and twos) and the way in which you can gain there is by using the same '0', '1' or '2' for each list member, instead of packing in the value you computed. Perhaps like this:

  # Version that uses *lots* of memory
  set mainlist {}
  for {set i 0} {$i<1000000} {incr i} {
     lappend mainlist [expr {$i%3}]

  # Version that uses much less...
  set cache {0 1 2}
  set mainlist {}
  for {set i 0} {$i<1000000} {incr i} {
     lappend mainlist [lindex $cache [expr {$i%3}]]

Tcl uses tricks like this internally when you use [ binary split ] to try to minimise the number of objects generated...

RS As long as they don't get optimized out, here's two ways of producing a pure number, integer or float:

 expr {$x+0}
 expr {$x*1}