Re: C2124 is most disrespectful of IEEE floating point arithmetic

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



John Carson wrote:
[..] By
5.19/3
"An arithmetic constant expression shall satisfy the requirements for
an integral constant expression, except that
- floating literals need not be cast to integral or enumeration type,
- conversions to floating point types are permitted."

Thus 1.0f/0.0f is an "arithmetic constant expression". Is this the
same as a "constant expression" that would make the program
ill-formed. Section 5.19/2 appears to give the answer:

"Other expressions [including arithmetic constant expressions] are
considered constant-expressions only for the purpose of non-local
static object initialization (3.6.2)."

In

int main()
{
float f (1.0f/0.0f);
}

f is not a non-local static object, so 1.0f/0.0f is not a "constant
expression" and therefore the program is *not* ill-formed.

By this reasoning, however,

float f (1.0f/0.0f);
int main()
{
}

is ill-formed. Comeau, however, compiles this program too, so I am
still puzzled.

Compilers have bugs. Undefined behaviour is just that, undefined.
Combine the two and you can have any behaviour either due to compiler
vendor's intent or dispite it, and there is nothing that one could
deduce about code correctness by looking how compilers behave. The
only true sources of information are the Standard and time.

If we think that either VC++ or Comeau C++ misbehave, we need to talk
to the folks who make them. But even then, considering the situation
at hand, you are most likely simply get the response "the behaviour
[of the division by zero] is undefined by the Standard, so we decided
to define it as this..." followed by what you already know, Comeau
lets it slide, VC++ complains.

VC++ is known to be overprotective. I don't know about Comeau in that
regard. It is very likely that if you do get a response from VC++
team, it's going to be one of those "since the 1.f/0.f expression has
undefined behaviour in run-time, we diagnose it for you and protect
you from doing the wrong thing". Then you ask "what about a/b when
b is initialised to 0.f", and you'll hear "well, we're not gods, you
know".

As to the fact that Comeau compiles what seems to be an ill-formed
program, the answer is simple: compilers are allowed to do that. It
might be required to give a diagnostic, but I'm too lazy to find that
out.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


.



Relevant Pages

  • Re: Difficult example?
    ... and specification expressions are so greatly expanded ... known that it has this latent bug regarding logical operations ... expression, but the result is required to be a constant expression, ... The new compilers gfortran and g95 seem to try to handle constant ...
    (comp.lang.fortran)
  • Re: Preprocessor limitation workarounds
    ... exceeds the capabilities of a 32 bit int (for older compilers). ... A constant expression may be a floating point expression. ... I have used floating point constant expressions to determine integer constants for embedded applications. ...
    (comp.lang.c)
  • Re: C++-style treatment of const?
    ... the initializer is a constant expression. ... you could still declare an object "const" even if the ... I don't know of any C compilers that implement this as an extension, ...
    (comp.std.c)
  • Re: what does this do? UPSHIFT(I:I) = CHAR (ICHAR (STRING(I:I) ) - 40B)
    ... Steve Lionel wrote: ... I would hope most compilers could do that as a constant expression ... at compile time. ...
    (comp.lang.fortran)
  • Re: Complex litterals
    ... Such a constant expression shall be, ... arithmetic constant expression shall only convert arithmetic ... I had not thought about the fact that compound literals are listed as one of the types of postfix operators, when I made my previous comment. ... However, I now think that 6.6p8 should be interpreted as allowing a compound literal as an an arithmetic constant expression, so long as it's type and initialization expressions meet the other requirements of that section. ...
    (comp.std.c)