home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:18724 gnu.gcc.help:2787
- Path: sparky!uunet!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
- From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
- Newsgroups: comp.lang.c,gnu.gcc.help
- Subject: Re: How precise is preprocessor math? (I'm using gcc)
- Message-ID: <1992Dec20.235758.11774@thunder.mcrcim.mcgill.edu>
- Date: 20 Dec 92 23:57:58 GMT
- References: <EJH.92Dec15111402@khonshu.colorado.edu> <1992Dec18.104956.11433@flood.com>
- Organization: McGill Research Centre for Intelligent Machines
- Lines: 65
-
- In article <1992Dec18.104956.11433@flood.com>, tom@flood.com (Tom Chatt) writes:
- > volpe@ausable.crd.ge.com writes:
-
- >> 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.)
- > The compiler is allowed to evaluate routine calls at compile time???
- > I think not!
-
- In general, no; in the case of sin(), it is. sin() (and the rest of
- the stuff in ANSI's spec) are fully specified; the compiler is allowed
- to know everything there is to know about them, including, in this
- case, that sin() computes a value that depends on nothing but its
- argument, and how to compute that value, and hence actually do so.
-
- > Given that an arbitrary routine may be called repeatedly with a
- > constant argument without any guarantee that the return value will be
- > the same,
-
- Right. It's not allowed to do so except when calling routines it knows
- behave nicely, like sin(). (This all comes under the "as if" rule: the
- compiler is allowed to emit anything that behaves as if it were
- executed as written. Since a conforming program cannot tell the
- difference between actually calling sin() at run-time and compiling the
- resulting value into the code, the compiler is allowed to do the
- computation at compile time.)
-
- > If Chris Volpe was suggesting the more limited proposition that
- > certain standard calls, such as sin(), are "known" to the compiler,
-
- Isn't that what was said?
-
- >> (It probably isn't likely that it evaluates sin(70) at compile time,
- >> but it's allowed to.)
-
- Nothing about evaluating any other calls at compile time.
-
- > that's plausible, but still seems dubious. "sin" is not a resrved
- > word in C, and nothing would prevent me from writing my own "sin()"
- > routine which did something completely different.
-
- Since ANSI, it effectively is a reserved word, when it occurs as an
- identifier with external scope. You can write you own sin(), yes,
- provided you declare it with file scope, and in that case, the compiler
- is not justified in constant-folding it. (I think you may also have to
- either omit <math.h> or #undef sin; not sure.)
-
- > Even if [...], there would still be different implementations, and a
- > compiler would be presumptuous to pick one for me.
-
- Huh? sin() computes the sine function. The implementation is
- irrelevant; only the return value matters. The compiler may use a
- completely different implementation from the run-time version, but *you
- can't tell*, because the return value is - must be! - the same.
-
- > [T]he "sin" routine being used by the example above apparently
- > expects degrees.
-
- If so, that's a bug in the program. sin() is specced to take its
- argument in radians.
-
- der Mouse
-
- mouse@larry.mcrcim.mcgill.edu
-