home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!mips!pacbell.com!att!cbnewsm!grenache!gah
- From: gah@grenache (George A. Howlett)
- Newsgroups: comp.lang.tcl
- Subject: Re: bug in expr?
- Message-ID: <1992Jul22.163913.27329@cbnewsm.cb.att.com>
- Date: 22 Jul 92 16:39:13 GMT
- References: <1992Jul21.215415.23716@crd.ge.com>
- Sender: news@cbnewsm.cb.att.com (NetNews Administrator)
- Reply-To: george.howlett@att.com
- Organization: AT&T Bell Laboratories
- Lines: 78
- X-Newsreader: Tin 1.1 PL4
- Nntp-Posting-Host: grenache.cnet.att.com
-
- kennykb@dssv01.crd.ge.com (Kevin B. Kenny) writes:
- :
- : Can someone explain the following? More to the point, can someone fix it?
- :
- : wish: set x [expr 400000.0+500000.0]
- : 900000
- : wish: set y [expr $x*$x]
- : -1748818944
- :
- : The square of 900000.0 oughtn't to be a large negative number.
- :
- :
- : 73 de ke9tv/2, Kevin There isn't any .signature virus, is there?
-
- I think the point that Kevin is making is that if you started out
- with floating point values, you should get a floating point result.
- The implicit demotion of the sum of two floating point numbers into
- a integer result is unexpected. It's fairly easy to detect here,
- but insidiously hard to track in more complex usages.
-
- Another problem, noted in an earlier posting by Brian Smith:
- : Here's an annoying bug in tcl dealing with double precision arithmetic.
- : Not really a "bug", so much, but a lousy feature. It seems the expr
- : command is intent on rounding off all double precesions values to about
- : 6 significant figures, whereas 14-15 are present on most machines. For
- : example, "expr 123456.789012345" returns "123457", but the command
- : "expr 123456.789012345-123456.7890123" returns "4.50091e-08", which is
- : pretty reasonable.
- :
- : Easily fixed (chang "%g" to "%lf" in tclExpr.c), but annoying if you're
- : trying to do accurate arithmetic.
- :
- : --
- : -----
- : Brian C. Smith
- : bsmith@cs.Berkeley.EDU
- : University of California, Berkeley
- : Computer Sciences Department
- : (510)642-9585
-
-
- You could simply change the format in tclExpr.c. Unfortunately,
- this still doesn't address the round-off problem and could break
- existing code which relies on the implicit demotion or a compact
- representation of a floating point number.
-
- The solution I adopted has the same mechanism as "awk". In awk, the
- format of the converted floating point number is controlled by the
- variable OFMT. Setting this variable changes the format conversion.
- The default format is "%g".
-
- In Kevin's example:
-
- wish: set OFMT %#g
- %#g
- wish: set x [expr 400000.0+500000.0]
- 900000.
- wish: set y [expr $x*$x]
- 8.10000e+11
- wish: unset OFMT
- wish: set x [expr 400000.0+500000.0]
- 900000
-
- This way one can set the format for the problem at hand without
- breaking existing code.
-
- I can repost this patch if anyone's interested.
-
- BTW: If you are using the patch to add math library functions,
- you could have explicitly promoted x using the "float()"
- function.
-
- wish: set x [expr 400000.0+500000.0]
- 900000
- wish: set y [expr float($x)*$x]
- 8.1e+11
-
- --gah
-