Re: NaNs
* Alan McKenney:
On Jul 28, 4:34 am, Thomas Richter <t...@math.tu-berlin.de> wrote:
Tom wrote:
I have some questions regarding NaNs for floating-point types:
First, am I correct that NaNs are NOT trap values, that is reading a NaN
itself (but not using it in an arithmetic expression) does not
automatically yield UB?
No, that's not correct. There are two types of NaNs, non-signaling and
signaling NaNs. Signalling NaNs are "trap-values".
I guess the following snippet below is OK, correct?:
...
// some code
if (X == std::numeric_limits<double>::quiet_NaN())
... since there is more than one type of NaN, test for
equality is a bad idea....
More to the point, there are a very large
number of bit patterns that count as NaN.
Both Thomas and Alan and OP Tom, please note that (1) number of bitpatterns is
irrelevant, because (2) an IEEE NaN is required to be unequal to anything,
including itself.
Correct testing, after having established IEEE floating point, is
if( x != x )
{
// This is a NaN
}
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]