Re: C2124 is most disrespectful of IEEE floating point arithmetic
- From: "Dr Pizza" <DrPizza@xxxxxxxxxxxxxxxxx>
- Date: Mon, 28 Aug 2006 10:01:17 -0700
Doug Harrison [MVP] wrote:
On Sun, 27 Aug 2006 17:51:26 -0700, "Dr Pizza"
<DrPizza@xxxxxxxxxxxxxxxxx> wrote:
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());
Division by zero is undefined, so the compiler can do whatever it
wants. For a compile-time expression, emitting an error is highly
appropriate. Using numeric_limits as above is self-documenting and
portable, and it is highly preferable to 1.0/0.0.
But it isn't undefined. Well; it's undefined in standard mathematics.
It isn't undefined in IEC 559, and VC++ purports to adhere to IEC 559.
IEEE 754 says that in the case of division of a finite value by zero:
<quotation>
7.2 Division by Zero
If the divisor is zero and the dividend is a finite nonzero number,
then the division by zero exception shall be signaled. The result, when
no trap occurs, shall be a correctly signed [Infinity] (6.3).
</quotation>
So depending on our handling of FP exceptions we should see one of two
things: with exceptions disabled, a signed infinity; with exceptions
enabled a divide by zero exception.
Yet in this case we get neither a trap nor a correctly signed infinity.
Instead, we have a compile-time error.
This is in spite of std::numeric_limits<float>::is_iec559 being 'true'
for float.
I don't see how you can have it both ways. Either is_iec559 should be
'false'--in which case all bets are off as to the behaviour of these
types and they are indeed "undefined", and a compile-time error is
consistent (even if not at all useful)--or the compiler should refrain
from trapping the division at compile-time, and instead allow the
defined runtime behaviour to occur.
I don't see why using numeric_limits is "highly preferable", as 1.0/0.0
is concise, well-defined, and clear.
--
.
- Follow-Ups:
- Re: C2124 is most disrespectful of IEEE floating point arithmetic
- From: Alexander Grigoriev
- Re: C2124 is most disrespectful of IEEE floating point arithmetic
- From: Abdo Haji-Ali
- Re: C2124 is most disrespectful of IEEE floating point arithmetic
- References:
- C2124 is most disrespectful of IEEE floating point arithmetic
- From: Dr Pizza
- Re: C2124 is most disrespectful of IEEE floating point arithmetic
- From: Doug Harrison [MVP]
- C2124 is most disrespectful of IEEE floating point arithmetic
- Prev by Date: Re: C2124 is most disrespectful of IEEE floating point arithmetic
- Next by Date: Re: VC8 build much slower than VC7
- Previous by thread: Re: C2124 is most disrespectful of IEEE floating point arithmetic
- Next by thread: Re: C2124 is most disrespectful of IEEE floating point arithmetic
- Index(es):
Relevant Pages
|