This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Don't constant fold 1.0/0.0 at compile-time
- From: Fergus Henderson <fjh at cs dot mu dot OZ dot AU>
- To: Segher Boessenkool <segher at koffie dot nl>
- Cc: Roger Sayle <roger at eyesopen dot com>, gcc-patches at gcc dot gnu dot org, Richard Henderson <rth at redhat dot com>
- Date: Mon, 7 Jul 2003 14:18:24 +1000
- Subject: Re: [PATCH] Don't constant fold 1.0/0.0 at compile-time
- References: <Pine.LNX.4.44.0307010122560.27458-100000@www.eyesopen.com> <3F04FCE7.6070407@koffie.nl>
On 04-Jul-2003, Segher Boessenkool <segher@koffie.nl> wrote:
> Roger Sayle wrote:
> > Could one of the language lawyers post their opinions on "x = 1.0/0.0"?
>
> If x has static storage duration, no exceptions are raised.
> See C99, F.7.4.
Right. However, a warning should be issued.
See C99 section F.7.2:
| Recommended practice
|
| [#2] The implementation should produce a diagnostic message
| for each translation-time floating-point exception, other
| than ``inexact'';305) the implementation should then proceed
| with the translation of the program.
In C++, the program is ill-formed, so a diagnostic is required,
and the diagnostic should be an error, not a warning.
See C++98 section 5 - Expressions [expr]:
| -5- If during the evaluation of an expression, the result is not
| mathematically defined or not in the range of representable values for
| its type, the behavior is undefined, unless such an expression is a
| constant expression (expr.const), in which case the program is
| ill-formed.
The above is all assuming you meant "static double x = 1.0/0.0;".
For the case of "auto double x = 1.0/0.0;" or "static double x; x = 1.0/0.0;"
the answer would be different. For C, such expressions should be evaluated
at run-time, raising a floating-point exception and potentially trapping etc.
For C++, it's not clear whether section 5[expr] paragraph 5 is supposed
to apply to any expression which has the form of a constant expression,
or only to expressions where the language requires a constant expression,
such as initializers for static constants. With the former interpretation,
a program containing "auto double x = 1.0/0.0;" would be ill-formed, but
with the latter interpretation it would not.
This ambiguity in C++98 is the topic of defect report #261. The proposed
response to that defect report is "The committee believes that the standard
is clear enough." -- which unfortunately does not answer the question!
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.