From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: border2.nntp.dca3.giganews.com!backlog4.nntp.dca3.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: How to check a Float for NaN Date: Thu, 22 May 2014 10:09:02 +0200 Organization: cbb software GmbH Message-ID: <3tso4mcv80hk.8j7e1grtnha0$.dlg@40tude.net> References: <3132e38d-18bb-4890-9cec-31056ac6e3ba@x19g2000prg.googlegroups.com> <83ce619a-beef-447f-91ef-ff3dd68ff9df@googlegroups.com> Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: QTaafVZuunHujkJPndFR7g.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 X-Original-Bytes: 2525 Xref: number.nntp.dca.giganews.com comp.lang.ada:186558 Date: 2014-05-22T10:09:02+02:00 List-Id: On Thu, 22 May 2014 00:27:02 -0700 (PDT), jan.de.kruyf@gmail.com wrote: > On Wednesday, April 30, 2008 10:36:21 PM UTC+2, Jerry wrote: > >> >> Check this out: >> >> function Is_NaN(x : Long_Float) return Boolean is >> begin >> return x /= x; >> end Is_NaN; >> >> A couple of minutes on Wikipedia saves the day. From >> http://en.wikipedia.org/wiki/NaN#NaN_encodings: >> >> "A NaN does not compare equal to any floating-point number or NaN, >> even if the latter has an identical representation. One can therefore >> test whether a variable has a NaN value by comparing it to itself." >> >> Jerry > > This worked on the Gnat compiler, this code > > if Cos_Theta /= Cos_Theta then > Gct.Trace (Debug_Str, "cos_theta is NaN******"); > else > Gct.Trace (Debug_Str, "cos_theta : " & Long_Float'Image (Cos_Theta)); > end if; > > gives this in the log: > > [MATH3D.DEBUG] 1/372 cos_theta is NaN****** (2014-05-22 09:23:24.155)(loc: math3d.adb:129) > > Hope it helps someone; You could simply use range check: X in Long_Float'Range NaN is not a number and thus outside the range. However, for technical computations, it is advisory to turn IEEE semantics off. Fortunately there is a very simple way to do so in Ada: subtype Real_Long_Float is Long_Float range Long_Float'Range; Use this instead of Long_Float and you will never have NaN, +/-Inf again. Constraint_Error will propagate instead. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de