home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / tcl / 2232 < prev    next >
Encoding:
Text File  |  1992-12-22  |  3.9 KB  |  77 lines

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!news.mtholyoke.edu!jbotz
  3. From: jbotz@mtholyoke.edu (Jurgen Botz)
  4. Subject: Re: Advice wanted on math functions for Tcl 7.0
  5. Message-ID: <Bzov4L.8wo@mtholyoke.edu>
  6. Sender: news@mtholyoke.edu (USENET News System)
  7. Organization: Mount Holyoke College
  8. References: <1h5pseINNkvu@agate.berkeley.edu>
  9. Date: Wed, 23 Dec 1992 01:36:21 GMT
  10. Lines: 65
  11.  
  12. In article <1h5pseINNkvu@agate.berkeley.edu> ouster@sprite.Berkeley.EDU (John Ousterhout) writes:
  13. >There are two possible ways to implement math functions: embedded into
  14. >expr or as separate commands, and I can't make up my mind about which
  15. >way to do it.
  16.  
  17. I vote for the embedded implementation... I had to think about this for a
  18. bit, and I came up with the following reasons:
  19.  
  20. 1) Natural syntax.  Programs that use transcendental functions are usually
  21.    doing failry complex math, and readability can make a _big_ difference
  22.    in debugging and allowing others to understand your code.
  23.  
  24. 2) Speed.  Transcendentals aren't needed very often, but when they are
  25.    speed usually is important.  It's true that where Tcl is used it's
  26.    usually not speed-critical, but a Tcl expression doing some reasonably
  27.    sophisticated math needn't execute significantly slower than equivalent
  28.    C code since most of the time is going to be spent in the math library
  29.    anyway.  If you have such an expression in a loop, why slow it down
  30.    unnecessarily with lots of back-and-forth string-number conversions?
  31.  
  32. 3) Data types.  Tcl has only one data type: strings.  Mathematical
  33.    expressions evaluated by computers on the other hand have many data
  34.    types with different representations and precisions.  What happens
  35.    when you evaluate an expression depends a whole lot on the the
  36.    internal implementation of these types in your evaluator, and as
  37.    such should be encapsulated by the evaluator.  True, strings can be
  38.    viewed as a super-set of all the possible internal data types of
  39.    the expression evaluator, and thus are loss-less as an intermediate
  40.    or external representation, but I think that encapsulating math
  41.    funcations in the expression evaluator keeps the Tcl "typeless"
  42.    paradigm cleaner
  43.  
  44.    For example, I might want to use Tcl in an application that does
  45.    arbitrary-precision arithmetic... if all things that inherently use
  46.    specific precision intermediate representations are encapsulated in
  47.    the "expr" command I can use arbitrary precision arithmetic with
  48.    higher-precision transcendentals by simply supplying my own
  49.    "apexpr" command.  In other words, the supplied transcendentals
  50.    simply don't make much sense outside of the context of the supplied
  51.    expression evaluator.
  52.  
  53.    As another example, let me address a point John specifically listed
  54.    as a possible advantage of an external implementation of transcendentals:
  55.  
  56. >4. In the command approach it's possible to use the individual math
  57. >   functions separately without having to go through expr.  For example,
  58. >   you could say "set a [cos $x]", whereas in the embedded approach
  59. >   you'd have to say "set a [expr cos($x)]"
  60.  
  61.    But indeed the second form makes more sense because what I'm going to
  62.    get back from the command is a string that represents an "expression
  63.    result", i.e. the external representation of the data type "expr" is
  64.    capable of returning.  It is then sufficient for me to know that "expr"
  65.    will return results with a certain precision and in a certain format,
  66.    whereas in the first form I have to specifically know the format and
  67.    precision of the result for each transcendental command (the fact that
  68.    they are likely to all be the same is incidental, since they needn't
  69.    be as string values... they could even be rational numbers, say in
  70.    format "dddd/dddd".)
  71.  
  72. --
  73. Jurgen Botz                  |   Internet: JBotz@mtholyoke.edu
  74. Academic Systems Consultant  |     Bitnet: JBotz@mhc.bitnet
  75. Mount Holyoke College        |      Voice: (US) 413-538-2375 (daytime)
  76. South Hadley, MA, USA        | Snail Mail: J. Botz, 01075-0629
  77.