home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / tcl / 1025 < prev    next >
Encoding:
Internet Message Format  |  1992-07-22  |  2.9 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!mips!pacbell.com!att!cbnewsm!grenache!gah
  2. From: gah@grenache (George A. Howlett)
  3. Newsgroups: comp.lang.tcl
  4. Subject: Re: bug in expr?
  5. Message-ID: <1992Jul22.163913.27329@cbnewsm.cb.att.com>
  6. Date: 22 Jul 92 16:39:13 GMT
  7. References: <1992Jul21.215415.23716@crd.ge.com>
  8. Sender: news@cbnewsm.cb.att.com (NetNews Administrator)
  9. Reply-To: george.howlett@att.com
  10. Organization: AT&T Bell Laboratories
  11. Lines: 78
  12. X-Newsreader: Tin 1.1 PL4
  13. Nntp-Posting-Host: grenache.cnet.att.com
  14.  
  15. kennykb@dssv01.crd.ge.com (Kevin B. Kenny) writes:
  16. : Can someone explain the following?  More to the point, can someone fix it?
  17. : wish: set x [expr 400000.0+500000.0]
  18. : 900000
  19. : wish: set y [expr $x*$x]
  20. : -1748818944
  21. : The square of 900000.0 oughtn't to be a large negative number.
  22. : 73 de ke9tv/2, Kevin          There isn't any .signature virus, is there?
  23.  
  24. I think the point that Kevin is making is that if you started out
  25. with floating point values, you should get a floating point result.
  26. The implicit demotion of the sum of two floating point numbers into
  27. a integer result is unexpected.  It's fairly easy to detect here,
  28. but insidiously hard to track in more complex usages. 
  29.  
  30. Another problem, noted in an earlier posting by Brian Smith:
  31. : Here's an annoying bug in tcl dealing with double precision arithmetic.
  32. : Not really a "bug", so much, but a lousy feature.  It seems the expr
  33. : command is intent on rounding off all double precesions values to about
  34. : 6 significant figures, whereas 14-15 are present on most machines.  For
  35. : example, "expr 123456.789012345" returns "123457", but the command
  36. : "expr 123456.789012345-123456.7890123" returns "4.50091e-08", which is
  37. : pretty reasonable.
  38. :
  39. : Easily fixed (chang "%g" to "%lf" in tclExpr.c), but annoying if you're
  40. : trying to do accurate arithmetic.
  41. :
  42. : --
  43. : -----
  44. : Brian C. Smith
  45. : bsmith@cs.Berkeley.EDU
  46. : University of California, Berkeley
  47. : Computer Sciences Department
  48. : (510)642-9585
  49.  
  50.  
  51. You could simply change the format in tclExpr.c.  Unfortunately, 
  52. this still doesn't address the round-off problem and could break
  53. existing code which relies on the implicit demotion or a compact
  54. representation of a floating point number.
  55.  
  56. The solution I adopted has the same mechanism as "awk".  In awk, the
  57. format of the converted floating point number is controlled by the
  58. variable OFMT.  Setting this variable changes the format conversion.
  59. The default format is "%g".
  60.  
  61. In Kevin's example:
  62.  
  63. wish: set OFMT %#g
  64. %#g
  65. wish: set x [expr 400000.0+500000.0]
  66. 900000.
  67. wish: set y [expr $x*$x]
  68. 8.10000e+11
  69. wish: unset OFMT
  70. wish: set x [expr 400000.0+500000.0]
  71. 900000
  72.  
  73. This way one can set the format for the problem at hand without
  74. breaking existing code.
  75.  
  76. I can repost this patch if anyone's interested.
  77.  
  78. BTW: If you are using the patch to add math library functions,
  79.      you could have explicitly promoted x using the "float()"
  80.      function.
  81.  
  82. wish: set x [expr 400000.0+500000.0]
  83. 900000
  84. wish: set y [expr float($x)*$x]
  85. 8.1e+11
  86.  
  87. --gah
  88.