home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18478 < prev    next >
Encoding:
Internet Message Format  |  1992-12-16  |  2.4 KB

  1. Xref: sparky comp.lang.c:18478 gnu.gcc.help:2759
  2. Path: sparky!uunet!crdgw1!rdsunx.crd.ge.com!bart!volpe
  3. From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
  4. Newsgroups: comp.lang.c,gnu.gcc.help
  5. Subject: Re: How precise is preprocessor math? (I'm using gcc)
  6. Message-ID: <1992Dec16.185400.20204@crd.ge.com>
  7. Date: 16 Dec 92 18:54:00 GMT
  8. References: <EJH.92Dec15111402@khonshu.colorado.edu>
  9. Sender: volpe@bart (Christopher R Volpe)
  10. Reply-To: volpe@ausable.crd.ge.com
  11. Organization: GE Corporate Research & Development
  12. Lines: 51
  13. Nntp-Posting-Host: bart.crd.ge.com
  14.  
  15. In article <EJH.92Dec15111402@khonshu.colorado.edu>, ejh@khonshu.colorado.edu (Edward J. Hartnett) writes:
  16. |> 
  17. |> Hi! I had some calculations in my program to arrive at a constant that
  18. |> I need to map some latitudes and longitudes to an i j grid. I was
  19. |> wondering if I could use a #define and save my cpu the task of
  20. |> repeatedly calculating this. Here's the code in which it is computed
  21. |> both with a #define and in the code:
  22.  
  23. You misunderstand how the preprocessor works. A "#define" causes textual
  24. substitution that takes place long before expressions are parsed and 
  25. evaluated, even when the expressions are evaluated at compile time.
  26.  
  27. |> 
  28. |>   double q, x, y, rho, Rg;
  29. |> #define Q1 2.*6371.0/GRIDSPACE_KM*(1.+sin(70.))
  30.  
  31. Q1 is not being defined as the value of that expression, it's being
  32. defined as the expression itself.
  33.  
  34. |>   Rg = 6371.0/GRIDSPACE_KM; 
  35. |>   q = 2.*Rg*(1.+sin(70.));
  36. |>   printf("Q1=%g  q=%g\n",Q1,q);
  37.  
  38. This simply expands to:
  39.      printf("Q1=%g q=%q\n", 2.*6371.0/GRIDSPACE_KM*(1.+sin(70.)), q);
  40.  
  41. And then the compiler evaluates it, or more likely, just the
  42. constant expression part of it. (It probably isn't likely that it
  43. evaluates sin(70) at compile time, but it's allowed to.) Both expressions
  44. are being evaluated at the same translation (or execution) phases. 
  45. The fact that you use a "#define" doesn't cause any more of that expression
  46. evaluation to be evaluated at compile time.
  47.  
  48. |> 
  49. |> and the result of the printf:
  50. |> Q1=113.015  q=113.015
  51. |> 
  52. |> So it seems I can do this in define. I was suprised. My question is,
  53. |> to what precision is this calculation done? I looked around in the
  54. |> preprocessor info file but couldn't turn up anything that seemed to
  55. |> bear on this question. Any help appreciated.
  56. |> --
  57. |>     Don't blame me, I voted against Amendment 2!
  58. |> 
  59. |> Edward Hartnett                ejh@khonshu.colorado.edu
  60.  
  61. -- 
  62. ==================
  63. Chris Volpe
  64. G.E. Corporate R&D
  65. volpecr@crd.ge.com
  66.