Version 10 of tcl_precision

Updated 2010-07-02 14:03:04 by sbron

A global control variable.

This variable controls the number of digits to generate when converting floating-point values to strings. It defaults to 12. 17 digits is "perfect" for IEEE floating-point in that it allows double-precision values to be converted to strings and back to binary with no loss of information. However, using 17 digits prevents any rounding, which produces longer, less intuitive results. For example, expr 1.4 returns 1.3999999999999999 with tcl_precision set to 17, vs. 1.4 if tcl_precision is 12.

KBK Note that if TIP #132 [L1 ] passes, then this problem will be a good bit less severe. On a Tcl interpreter with a trial implementation of TIP #132, we get:

 % set tcl_precision 17
 17
 % expr 1.4
 1.4

Note that this change doesn't affect the inherent limitations to the precision of binary floating point numbers: double(1.4) is still less than the Platonic real number 7/5. This can be proven by multiplying it by three:

 % expr { 3. * 1.4 }
 4.199999999999999

(Interestingly enough, multiplying by five yields an exact result:)

 % expr { 5. * 1.4 }
 7.0

All interpreters in a process share a single tcl_precision value: changing it in one interpreter will affect all other interpreters as well. However, safe interpreters are not allowed to modify the variable. (from: TclHelp)


Donald Porter's wise advice, posted to the comp.lang.tcl newsgroup, is (edited slightly): "It is best to use tcl_precision in a 'global' way. Set it early in your program's execution, then leave it alone. For situations like above where you want to control precision of single assignment, use format.

If you're doing any math at all where you care about getting the right answer, you will just [set ::tcl_precision 17], and then forget anyone suggested anything else."


RS: The funny thing about tcl_precision is that it's not there in the beginning - it is just created on demand (trace?):

 % info vars
 tcl_rcFileName tcl_version argv argv0 tcl_interactive auto_oldpath auto_path err
 orCode errorInfo auto_index env tcl_patchLevel argc tcl_libPath tcl_platform tcl
 _library
 % set tcl_precision
 12
 % info vars
 tcl_rcFileName tcl_version argv argv0 tcl_interactive auto_oldpath auto_path err
 orCode tcl_precision errorInfo auto_index env tcl_patchLevel argc tcl_libPath tc
 l_platform tcl_library
 % unset tcl_precision
 % set tcl_precision
 12

Now you see it - now you don't. tcl_precision is the only variable I know that can be read after being unset.

DGP You don't know about tcl_interactive then? Common trait - they are Tcl_LinkVar variables, script level representations of C values.


LES: I don't see it written anywhere that 17 is the maximum precision allowed, so I am saying it now.


sbron -- KBK mentioned in the Tcl Chatroom that after TIP #132 a value can be displayed the same way as before by using format with a formatstring of %#.12g


[Category???]