From -1779852634473015963
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,d93488bb970aa444
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2001-06-09 22:04:02 PST
Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
From: Pete Becker <petebecker@acm.org>
Newsgroups: comp.std.c++
Subject: Re: How to test for NaNs and infinites?
Date: Sun, 10 Jun 2001 05:03:33 GMT
Organization: Dinkumware, Ltd
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <3B222BB6.3B86CF85@acm.org>
References: <3B176D2F.70C3E7F7@meteo.fr> <usrihtkqfjd9qfj2bgtbp6pj530asi567k@4ax.com> <b172eb2f.0106071330.5f984936@posting.google.com> <9fql8h$pfg$1@news.hal-pc.org> <3B21100E.5B25F7AA@acm.org> <9frdlp$14bc$1@news.hal-pc.org>
X-Trace: mail2news.demon.co.uk 992149419 mail2news:15909 mail2news mail2news.demon.co.uk
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Date: 9 Jun 2001 13:59:40 GMT
X-Accept-Language:  en
Lines: 79
Xref: archiver1.google.com comp.std.c++:5901

Greg Brewer wrote:
> 
> "Pete Becker" <petebecker@acm.org> wrote in message
> news:3B21100E.5B25F7AA@acm.org...
> > Greg Brewer wrote:
> > > > want to emulate C99's complex' behaviour. This requires me, as a
> > > > library writer, and not as an implementation writer, to test for NaN
> > > > (and infinites).
> > > > As things stand, and if I understand correctly, there is *no* way
> > > > for me to do that in a legal C++ way.
> > > Sure there is.
> > > bool IsNAN(double &d)
> > > {
> > >    union
> > >    {
> > >        double *d;
> > >        char *bytes;
> > >    } value;
> > >    value.d = &d;
> > >    // test value.bytes for NAN.
> > > }
> >
> > The behavior of code that stores to one field of a union and accesses
> > another is not defined by the C++ language specification. So "test
> > value.bytes for NaN" is not portable.
> 
> The request was for legal C++ not portable C++!

Fine. Read that as "So 'test value.bytes for NaN' is not legal." The
point was that even though it seems to work on some platforms, it is not
required to work, and sooner or later it will fail.

> 
> > > Finding the proper test proved to be the real chalenge.
> > Well, yes: aside from the type pun problem, how do you write that test
> > in portable C++?
> 
> The request was for legal C++ not portable C++!

There was no "request." There was a general comment about how to do this
sort of thing, and what I said is correct and relevant.

> Testing is done by using
> masks
>    union
>    {
>      const double *d;
>      const long   *l;
>    } v;
>    v.d = &d;
>    // coded from Borland RTL for _isfinite
>    // NAN is a subset of INF
>    if ((v.l[1] & 0x7ff80000) == 0x7ff80000)  // infinite number
>       return -32000;
> works for me on my platform.
> 

Yes, that's an overly elaborate version of the usual technique for
detecting NaNs in 64-bit floating point values on Intel x86 hardware.
It's also compiler-specific, both because it assumes that doubles are
implemented using Intel 64-bit floating point values and because it
assumes that longs are 32 bits. Further, the two substantive comments
are wrong: NaN is not a subset of INF, and that test has nothing to do
with infinite numbers.

Now, do you really want to insist on absolute precision and narrow
topicality in every statement, or do you want to have a meaningful
discussion?

-- 
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]



