From 3678246128406048702 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,d93488bb970aa444 X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2001-06-08 14:39:01 PST Path: archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.icl.net!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail From: "Greg Brewer" Newsgroups: comp.std.c++ Subject: Re: How to test for NaNs and infinites? Date: Fri, 8 Jun 2001 21:38:21 GMT Organization: Houston Area League of PC Users Approved: Fergus Henderson , moderator of comp.std.c++ Message-ID: <9frdlp$14bc$1@news.hal-pc.org> References: <3B176D2F.70C3E7F7@meteo.fr> <9fql8h$pfg$1@news.hal-pc.org> <3B21100E.5B25F7AA@acm.org> X-Trace: mail2news.demon.co.uk 992036306 mail2news:27936 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) NNTP-Posting-Date: Fri, 8 Jun 2001 20:47:53 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.3018.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.3018.1300 Lines: 54 Xref: archiver1.google.com comp.std.c++:5883 "Pete Becker" 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++! > > 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++! 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. Greg Brewer --- [ 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 ]