home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / fortran / 3240 < prev    next >
Encoding:
Text File  |  1992-08-27  |  4.3 KB  |  108 lines

  1. Newsgroups: comp.lang.fortran
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Small Language Wanted
  5. Message-ID: <9224107.5975@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <DAVIS.92Aug23010605@pacific.mps.ohio-state.edu> <H.3lvmyc56w&A@lionbbs.bs.open.de> <1992Aug27.181926.5677@alchemy.chem.utoronto.ca>
  9. Date: Thu, 27 Aug 1992 21:41:04 GMT
  10. Lines: 96
  11.  
  12. mroussel@alchemy.chem.utoronto.ca (Marc Roussel) writes:
  13.  
  14. >In article <H.3lvmyc56w&A@lionbbs.bs.open.de> UweKloss@lionbbs.bs.open.de
  15. >writes:
  16. >>In <DAVIS.92Aug23010605@pacific.mps.ohio-state.edu>, "John E. Davis" writes:
  17. >>> In addition, C has nothing equivalent to Fortrans **
  18. >>> operator nor does it have complex types.
  19. >>The differeces between code the compiler generates and
  20. >>the one that is linked as a library or inserted by the
  21. >>preprocessor.
  22. >
  23. >     That is a big difference!  Consider the following Fortran program:
  24. >
  25. >      integer i
  26. >      double precision a,b
  27. >      a = 4.2
  28. >      b = 9.3
  29. >      i = 10
  30. >      write(*,*)a**i,a**b
  31. >      END
  32. >
  33. >     The compiler, faced with such a program, examines the types of the
  34. >arguments and generates completely different code to do the two
  35. >exponentiations; certainly, one calculation can be expressed exactly in
  36. >terms of multiplications while the other cannot.  It may be more
  37. >efficient or more accurate to do it one way or the other and the
  38. >compiler can choose a calculation method which reflects current thinking
  39. >on this sort of problem.
  40. >     Now consider an equivalent piece of code, written in C:
  41. >
  42. >     void main(){
  43. >     int i;
  44. >     double a,b;
  45. >     a = 4.2;
  46. >     b = 9.3;
  47. >     i = 10;
  48. >     printf(pow(a,(double) i),pow(a,b));
  49. >     }
  50. >
  51. >(You'll forgive me, I hope, for getting the C idiom slightly wrong.
  52. >The one manual I have at hand right now is less than entirely helpful on
  53. >several trivial details.  In particular, I'm not at all sure that the
  54. >cast of i to double is entirely kosher, and I think that printf wants a
  55. >format specification.  In any event, you get the general idea.)  Can the
  56. >compiler play the sorts of tricks which the Fortran compiler was free to
  57. >do?  I don't think so.
  58.  
  59. Well, you are wrong.
  60.  
  61. >pow() being a library routine, I don't think it
  62. >would be good form (or standard-conforming) for the compiler to guess at
  63. >the programmer's intent and to do anything more than to leave the
  64. >reference to be resolved by the linker.  After all, until the library is
  65. >linked in, in principle the compiler knows nothing about any function
  66. >but perhaps its prototype.  This particular library function
  67. >expects (I think) two doubles (floats?) and must be passed these.
  68. >That's all there is to it.
  69.  
  70. No, in principle if the library function in question is a *standard*
  71. library function, then its implementation is just a part of the
  72. compilation system. When the compiler sees #include <math.h>, it can
  73.     - read /usr/include/math.h
  74.     - read foobar.xyzzy
  75.     - insert the appropriate declarations without reading any file.
  76.  
  77. Furthermore, when it sees
  78.  
  79.     pow(a,i)
  80.  
  81. in your code, and "i" happens to be an integer then it can emit code to
  82.     - call the standard pow() function after convertin
  83.       i to a double
  84.     - call a special function the calculates doubles to
  85.       integer powers
  86.     - calculate the exponentiation using inline code.
  87. Ie. it can make EXACTLY the same optimizations that Fortran compilers
  88. make when they see a**i.
  89. In other words, the _ONLY_ differences between an operator and a function are
  90.     (1) the syntax is different
  91.     (2) for the function, you should include the appropriate header file
  92.         before using it.
  93.  
  94. >     In my opinion, what C lacks to be able to handle these things
  95. >correctly is any concept of a "built-in" function.  Fortran knows to
  96. >treat sin(a) and sin(x) differently if a is real and x is double
  97. >precision because knowledge of these primitive functions is built into
  98. >the compiler.  C can't do anything like this because of its design.
  99.  
  100. Yes it can.
  101. Any function that is part of the standard library is a "built-in" function.
  102.  
  103. -- 
  104. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  105. This .signature virus is a self-referential statement that is true - but 
  106. you will only be able to consistently believe it if you copy it to your own
  107. .signature file!
  108.