home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!agate!adrianho
- From: adrianho@barkley.berkeley.edu (Adrian J Ho)
- Newsgroups: comp.lang.tcl
- Subject: Re: bug in expr?
- Date: 22 Jul 92 04:09:54
- Organization: University of California, Berkeley
- Lines: 59
- Message-ID: <ADRIANHO.92Jul22040954@barkley.berkeley.edu>
- References: <1992Jul21.215415.23716@crd.ge.com>
- NNTP-Posting-Host: barkley.berkeley.edu
- In-reply-to: kennykb@dssv01.crd.ge.com's message of 21 Jul 92 21:54:15 GMT
-
- In article <1992Jul21.215415.23716@crd.ge.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.
-
- EXPLANATION: In the Expressions subsection of the Tcl(3) man page:
-
- [...] Where possible, operands are interpreted as integer
- values. [...] If an operand does not have one of the integer formats
- given above, then it is treated as a floating point number if that is
- possible. [...] If no numeric interpretation is possible, then an
- operand is left as a string (and only a limited set of operators may
- be applied to it.
-
- The first expression sets x to 900000 (not 900000.0), which is
- subsequently treated as an *integer* in the second expression. This
- is because the conversion of a floating-point number back to a string
- is done as follows:
-
- sprintf(valuePtr->pv.buffer, "%g", valuePtr->doubleValue);
- ^^
- This strips all trailing zeros, and the decimal point if the previous
- operation leaves nothing behind it.
-
- FIX: (1) set y [expr $x.0*$x.0] (kludge)
- (2) Change %g above to %e or %f. Disadvantage: Some numbers will
- look ugly (eg. 900000.0 is returned as 900000.000000). Apply
- the following patch (change %e to %f if that's your liking):
-
- *** tclExpr.c~ Mon Mar 23 09:54:06 1992
- --- tclExpr.c Wed Jul 22 04:03:17 1992
- ***************
- *** 1124,1130 ****
- if (valuePtr->type == TYPE_INT) {
- sprintf(valuePtr->pv.buffer, "%ld", valuePtr->intValue);
- } else if (valuePtr->type == TYPE_DOUBLE) {
- ! sprintf(valuePtr->pv.buffer, "%g", valuePtr->doubleValue);
- }
- valuePtr->type = TYPE_STRING;
- }
- --- 1124,1130 ----
- if (valuePtr->type == TYPE_INT) {
- sprintf(valuePtr->pv.buffer, "%ld", valuePtr->intValue);
- } else if (valuePtr->type == TYPE_DOUBLE) {
- ! sprintf(valuePtr->pv.buffer, "%e", valuePtr->doubleValue);
- }
- valuePtr->type = TYPE_STRING;
- }
-
- Any comments, John?
- --
- -----------------------------------------------------------------------------
- Adrian Ho, He With The Embarrassingly *Dead* Passion ** Phone: (510) 642-5563
- System Manager, CFD Lab, ME Dept, UC Berkeley * adrianho@barkley.berkeley.edu
- Maintainer, Tcl/Tk Contrib Archive ---- barkley.berkeley.edu [128.32.142.237]
- Send all Tcl/Tk Archive-related stuff to tcl-archive@barkley.berkeley.edu
-