home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / lisp / 2229 < prev    next >
Encoding:
Internet Message Format  |  1992-08-14  |  2.7 KB

  1. Path: sparky!uunet!dove!cam!ARTEMIS
  2. From: miller@FS1.cam.nist.gov (Bruce R. Miller)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: floating point precision
  5. Message-ID: <2922805766@ARTEMIS.cam.nist.gov>
  6. Date: 14 Aug 92 18:29:26 GMT
  7. References: <16fnshINN7db@early-bird.think.com>
  8. Sender: news@cam.nist.gov
  9. Organization: NIST - Computing and Applied Mathematics Laboratory
  10. Lines: 50
  11.  
  12.  
  13. In article <16fnshINN7db@early-bird.think.com>, Barry Margolin writes:
  14. > In article <1992Aug13.213615.28011@cs.yale.edu> yip-ken@CS.YALE.EDU (ken yip) writes:
  15. > >How can one specify the floating point precision of a numerical 
  16. > >routine?
  17. > All the mathematical functions in CL are generic, and will operate on any
  18. > floating point types, returning results of the same type (if multiple
  19. > arguments of different types are provided, it will coerce them all to the
  20. > largest one).  So, if you want to see the result of performing the
  21. > computation in double-float, provide double floats as the initial input.
  22.  
  23. That's completely true --- as far as it goes.  
  24.  
  25. But, you do pay a performance price for generic arithmetic, especially
  26. in inner loops.  This is complicated by numerical constants which appear
  27. in any non-trivial numerical code.
  28.  
  29. Even in a system with efficient dispatch which ignores type
  30. declarations, the cost of repeatedly converting rational constants to
  31. floats is significant (I dont have figures handy, though).  If you code
  32. them as single precision constants you loose accuracy when you give
  33. double-float inputs.  On the other hand, coding double-float constants
  34. infect the results and you'll double-float answers; they'll be
  35. expensive but most likely no more accurate than your single float
  36. results!
  37.  
  38. Of course, if run time is not a concern, then code it in generic, using
  39. rational constants.  
  40.  
  41. I recently asked advice here  on a similar problem and got some good help
  42. from Barry, Chris McConnel, Bob Kern and others (thanks!). 
  43.  
  44. The most elegant solution proposed involved code-walkers. Unfortunately,
  45. to do it right requires knowledge of _all_ functions to be encountered
  46. (ie. dependence of output types on input types). It would probably be
  47. easily piggybacked onto an inference scheme & database like Python, but
  48. I didn't feel like developing the database myself.
  49.  
  50. My solution was to develop a macro which extended the abstraction of
  51. generic arith.  It checks the types of some of the args and branches to
  52. the duplicate of the body which has been `optimized' for that type.
  53. There's a second macro to be used within the outer one which coerces a
  54. number or named constant to the appropriate type.
  55. The code that you write using this is a bit on the verbose side :< but
  56. it does work.
  57. [My version does no declarations, though.]
  58.  
  59. bruce
  60. miller@cam.nist.gov
  61.