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

  1. Newsgroups: comp.lang.fortran
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!pacific.mps.ohio-state.edu!davis
  3. From: davis@pacific.mps.ohio-state.edu ("John E. Davis")
  4. Subject: Re: Small Language Wanted
  5. In-Reply-To: davis@pacific.mps.ohio-state.edu's message of Fri, 28 Aug 1992 02:14:18 GMT
  6. Message-ID: <DAVIS.92Aug28015332@pacific.mps.ohio-state.edu>
  7. Sender: news@pacific.mps.ohio-state.edu
  8. Nntp-Posting-Host: pacific.mps.ohio-state.edu
  9. Reply-To: davis@pacific.mps.ohio-state.edu  (John E. Davis)
  10. Organization: "Dept. of Physics, The Ohio State University"
  11. References: <DAVIS.92Aug23010605@pacific.mps.ohio-state.edu>
  12.     <H.3lvmyc56w&A@lionbbs.bs.open.de>
  13.     <1992Aug27.181926.5677@alchemy.chem.utoronto.ca>
  14.     <9224107.5975@mulga.cs.mu.OZ.AU>
  15.     <DAVIS.92Aug27211418@pacific.mps.ohio-state.edu>
  16. Date: Fri, 28 Aug 1992 06:53:32 GMT
  17. Lines: 67
  18.  
  19.  
  20. In a previous article <DAVIS.92Aug27211418@pacific.mps.ohio-state.edu> I
  21. presented a C program which demonstrates that simply including math.h into a C
  22. program does not guarantee that `float y, x = 2.0; int i = 4; y = pow(x,i)'
  23. will produce the correct value for y as a previous poster suggested:
  24.  
  25.       >In other words, the _ONLY_ differences between an operator and a
  26.       >function are 
  27.       >  (1) the syntax is different
  28.       >  (2) for the function, you should include the appropriate header file
  29.       >      before using it.
  30.  
  31.  
  32. It has been pointed out to me by several people that this is compiler
  33. dependent and the a true ansi compliant answer will produce the correct
  34. answer. This is because instead of a declaration like:
  35.  
  36.                    extern double pow(/* double, double */);
  37.  
  38. the ANSI declaration would read:
  39.  
  40.                    extern double pow(double, double);
  41.  
  42.  
  43. This has the effect of performing the correct typecasts before calling the
  44. function `pow'.
  45.  
  46. But then this behavior contradicts something else the other poster said:
  47.  
  48.      >pow(a,i)
  49.  
  50.      >in your code, and "i" happens to be an integer then it can emit code to
  51.      >        - call the standard pow() function after convertin
  52.      >           i to a double
  53.      >        - call a special function the calculates doubles to
  54.      >          integer powers
  55.      >        - calculate the exponentiation using inline code.
  56.      >Ie. it can make EXACTLY the same optimizations that Fortran compilers
  57.      >make when they see a**i.
  58.      >[...]
  59.      >Any function that is part of the standard library is a "built-in"
  60.      >function. 
  61.  
  62. This seems to imply that if I do:
  63.  
  64. int i = 4; float y, x = 2.0;  y = pow(x,i)
  65.  
  66. The code for pow will recognize that 4.0 = (double) i is equivalent to the
  67. integer 4 and will generate code like:
  68.  
  69.        return(tmp = 2.0 * 2.0, tmp * tmp);
  70.  
  71. as opposed something like 
  72.  
  73.        return( exp ( 4.0 * log(2.0));
  74.  
  75. Are there really any existing C compliers out there that perform such
  76. optimizations?       
  77.  
  78. --
  79.      _____________
  80. #___/John E. Davis\_________________________________________________________
  81. #
  82. # internet: davis@amy.tch.harvard.edu
  83. #   bitnet: davis@ohstpy
  84. #   office: 617-735-6746
  85. #
  86.