From -3248845335094672675 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,d93488bb970aa444 X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2001-06-11 10:14:01 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail From: "Al Grant" Newsgroups: comp.std.c++ Subject: Re: How to test for NaNs and infinites? Date: Mon, 11 Jun 2001 17:13:18 GMT Organization: ARM Limited Approved: Fergus Henderson , moderator of comp.std.c++ Message-ID: <9g2d57$144$1@cam-news1.cambridge.arm.com> References: <3B176D2F.70C3E7F7@meteo.fr> X-Trace: mail2news.demon.co.uk 992279615 mail2news:816 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: 11 Jun 2001 12:21:59 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Lines: 39 Xref: archiver1.google.com comp.std.c++:5935 "Bill Gibbons" wrote in message news:bill-1006011032170001@adsl-64-172-170-154.dsl.snfc21.pacbell.net... > The most portable way I know is: > > bool IsNaN(double d) { > return (d != d); > } > > bool IsInfinity(double d) { > static const double PositiveInfinity = 1.0 / 0.0; > static const double NegativeInfinity = -1.0 / 0.0; > return (d == PositiveInfinity || d == NegativeInfinity); > } > > You might get compile-time warnings about divide by zero, but this approach > should work on any system which supports IEEE floating point. Even if you have dynamically turned off FP exception handling, the compiler will probably have generated code which works if exceptions are handled, i.e. which does the division the first time through IsInfinity (by testing a flag). You can avoid this expense by making the constants global. Alternatively: bool IsInfinity(double d) { return (d == d && (d - d) != 0.0); } --- [ 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 ]