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

  1. Path: sparky!uunet!cis.ohio-state.edu!ucbvax!ansa.co.uk!rtor
  2. From: rtor@ansa.co.uk (Owen Rees)
  3. Newsgroups: comp.lang.tcl
  4. Subject: Re: bug in expr?
  5. Message-ID: <9207221015.AA13447@yscydion.ansa.co.uk>
  6. Date: 22 Jul 92 10:15:22 GMT
  7. References: <1992Jul21.215415.23716@crd.ge.com>
  8. Sender: daemon@ucbvax.BERKELEY.EDU
  9. Lines: 51
  10.  
  11.  
  12. (Kevin B. Kenny) writes:
  13.  
  14. > Can someone explain the following?  More to the point, can someone fix it?
  15. > wish: set x [expr 400000.0+500000.0]
  16. > 900000
  17. > wish: set y [expr $x*$x]
  18. > -1748818944
  19. > The square of 900000.0 oughtn't to be a large negative number.
  20.  
  21. The square of 900000.0 isn't a large negative number e.g.:
  22.  
  23. wish: set x 900000.0
  24. 900000.0
  25. wish: set y [expr $x*$x]
  26. 8.1e+11
  27.  
  28. The problem is that you are squaring "900000" and not "900000.0" -
  29. don't forget that "Tcl supports only one type of data: strings." and
  30. that expr returns the result as a string. The 'insignificant' zeros
  31. and the decimal point have been stripped off because %g is used to
  32. convert the result to a string.
  33.  
  34. One thing you could do is use format to force the string to have its
  35. decimal point as in:
  36.  
  37. wish: set x [format "%#g" [expr 400000.0+500000.0]]
  38. 900000.
  39. wish: set y [expr $x*$x]
  40. 8.1e+11
  41.  
  42. or you could force the second expr to use floating point like this:
  43.  
  44. wish: set x [expr 400000.0+500000.0]
  45. 900000
  46. wish: set y [expr 1.0*$x*$x]
  47. 8.1e+11
  48.  
  49.  
  50. Owen.
  51.  
  52.      /\/\    Owen Rees <rtor@ansa.co.uk>
  53.     / /  \    APM, Poseidon House
  54.    / / /\ \    Castle Park, Cambridge
  55.   / / /\ \ \    CB3 0RD, UK
  56.  / /_/__\ \ \    Phone:    + 44 223 323010
  57. /__________\_\    Fax:    + 44 223 359779
  58.   A  N  S  A
  59.  
  60.