home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:18478 gnu.gcc.help:2759
- Path: sparky!uunet!crdgw1!rdsunx.crd.ge.com!bart!volpe
- From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
- Newsgroups: comp.lang.c,gnu.gcc.help
- Subject: Re: How precise is preprocessor math? (I'm using gcc)
- Message-ID: <1992Dec16.185400.20204@crd.ge.com>
- Date: 16 Dec 92 18:54:00 GMT
- References: <EJH.92Dec15111402@khonshu.colorado.edu>
- Sender: volpe@bart (Christopher R Volpe)
- Reply-To: volpe@ausable.crd.ge.com
- Organization: GE Corporate Research & Development
- Lines: 51
- Nntp-Posting-Host: bart.crd.ge.com
-
- In article <EJH.92Dec15111402@khonshu.colorado.edu>, ejh@khonshu.colorado.edu (Edward J. Hartnett) writes:
- |>
- |> Hi! I had some calculations in my program to arrive at a constant that
- |> I need to map some latitudes and longitudes to an i j grid. I was
- |> wondering if I could use a #define and save my cpu the task of
- |> repeatedly calculating this. Here's the code in which it is computed
- |> both with a #define and in the code:
-
- You misunderstand how the preprocessor works. A "#define" causes textual
- substitution that takes place long before expressions are parsed and
- evaluated, even when the expressions are evaluated at compile time.
-
- |>
- |> double q, x, y, rho, Rg;
- |> #define Q1 2.*6371.0/GRIDSPACE_KM*(1.+sin(70.))
-
- Q1 is not being defined as the value of that expression, it's being
- defined as the expression itself.
-
- |> Rg = 6371.0/GRIDSPACE_KM;
- |> q = 2.*Rg*(1.+sin(70.));
- |> printf("Q1=%g q=%g\n",Q1,q);
-
- This simply expands to:
- printf("Q1=%g q=%q\n", 2.*6371.0/GRIDSPACE_KM*(1.+sin(70.)), q);
-
- And then the compiler evaluates it, or more likely, just the
- constant expression part of it. (It probably isn't likely that it
- evaluates sin(70) at compile time, but it's allowed to.) Both expressions
- are being evaluated at the same translation (or execution) phases.
- The fact that you use a "#define" doesn't cause any more of that expression
- evaluation to be evaluated at compile time.
-
- |>
- |> and the result of the printf:
- |> Q1=113.015 q=113.015
- |>
- |> So it seems I can do this in define. I was suprised. My question is,
- |> to what precision is this calculation done? I looked around in the
- |> preprocessor info file but couldn't turn up anything that seemed to
- |> bear on this question. Any help appreciated.
- |> --
- |> Don't blame me, I voted against Amendment 2!
- |>
- |> Edward Hartnett ejh@khonshu.colorado.edu
-
- --
- ==================
- Chris Volpe
- G.E. Corporate R&D
- volpecr@crd.ge.com
-