home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cis.ohio-state.edu!ucbvax!ansa.co.uk!rtor
- From: rtor@ansa.co.uk (Owen Rees)
- Newsgroups: comp.lang.tcl
- Subject: Re: bug in expr?
- Message-ID: <9207221015.AA13447@yscydion.ansa.co.uk>
- Date: 22 Jul 92 10:15:22 GMT
- References: <1992Jul21.215415.23716@crd.ge.com>
- Sender: daemon@ucbvax.BERKELEY.EDU
- Lines: 51
-
-
- (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.
-
- The square of 900000.0 isn't a large negative number e.g.:
-
- wish: set x 900000.0
- 900000.0
- wish: set y [expr $x*$x]
- 8.1e+11
-
- The problem is that you are squaring "900000" and not "900000.0" -
- don't forget that "Tcl supports only one type of data: strings." and
- that expr returns the result as a string. The 'insignificant' zeros
- and the decimal point have been stripped off because %g is used to
- convert the result to a string.
-
- One thing you could do is use format to force the string to have its
- decimal point as in:
-
- wish: set x [format "%#g" [expr 400000.0+500000.0]]
- 900000.
- wish: set y [expr $x*$x]
- 8.1e+11
-
- or you could force the second expr to use floating point like this:
-
- wish: set x [expr 400000.0+500000.0]
- 900000
- wish: set y [expr 1.0*$x*$x]
- 8.1e+11
-
-
- Owen.
-
- /\/\ Owen Rees <rtor@ansa.co.uk>
- / / \ APM, Poseidon House
- / / /\ \ Castle Park, Cambridge
- / / /\ \ \ CB3 0RD, UK
- / /_/__\ \ \ Phone: + 44 223 323010
- /__________\_\ Fax: + 44 223 359779
- A N S A
-
-