Re: C2124 is most disrespectful of IEEE floating point arithmetic



John Carson wrote:

"Dr Pizza" <DrPizza@xxxxxxxxxxxxxxxxx> wrote in message
news:xn0eqhw1a4lwx05000@xxxxxxxxxxxxxxxxxxxx
Since VC++ purports to follow IEC 559, and IEC 559 defines the
division of a non-zero by a zero to be a signed infinity, isn't it
rather silly that:

float inf(1.0f / 0.0f);

should result in a compile-time error, C2124? An error may be
appropriate for integer arithmetic (as it'll generate a runtime SEH
exception), but for floating point arithmetic it's surely incorrect
behaviour?

What gives? Am I being unreasonable in my expectations? It's
certainly rather nicer to type the above than have to spew:

float inf(std::numeric_limits<float>::infinity());


For what it is worth, this compiles:

float zero = 0.0f;
float f(1.0f/zero);

as does

const float zero = 0.0f;
float f(1.0f/zero);

It is apparently only a literal zero that gives a problem.

Weird. I had expected that the compiler would propagate the constant
zero and emit the same error, but looking at the generated assembly it
doesn't; it performs a runtime division.

--

.



Relevant Pages

  • Re: Interesting math
    ... precision when a number is zero. ... A C Float Number must have six or more significant digits ... and read about C Float Numbers and programming in C language. ...
    (alt.usage.english)
  • fpu code optimisation request
    ... Consider the following pseudo-code function: ... function myAND(float a, float b) ... Or, in "normal" language, zero stays zero, ...
    (comp.lang.asm.x86)
  • Re: is it safe to zero float array with memset?
    ... > No, but it's "safe enough". ... > that doesn't use all bits zero for float 0.0. ... If I were going to use memset() to zero an array of floats, ...
    (comp.lang.c)
  • Re: C2124 is most disrespectful of IEEE floating point arithmetic
    ... of a non-zero by a zero to be a signed infinity, ... float inf; ... Division by zero is undefined, so the compiler can do whatever it wants. ... For a compile-time expression, emitting an error is highly appropriate. ...
    (microsoft.public.vc.language)
  • RE: VFP 9: Making transform() Drop Decimal 0s
    ... Numeric (includes Double, Float, or Integer data types): ... value is less than one but greater than negative one, zero is included before ... Transform() to the worse! ...
    (microsoft.public.fox.programmer.exchange)