comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: How to check a Float for NaN
Date: Thu, 22 May 2014 08:28:33 -0700 (PDT)
Date: 2014-05-22T08:28:33-07:00	[thread overview]
Message-ID: <6c8f74c9-4b4e-47a0-90e0-efa1ecdd5e2e@googlegroups.com> (raw)
In-Reply-To: <uoa7quysodns.g4itegyo3862$.dlg@40tude.net>

On Thursday, May 22, 2014 2:48:47 AM UTC-7, Dmitry A. Kazakov wrote:
> On Thu, 22 May 2014 10:24:35 +0100, Simon Wright wrote:
> 
> > "Dmitry A. Kazakov" writes:
> 
> >> On Thu, 22 May 2014 00:27:02 -0700 (PDT), jan.de.kruyf wrote:

> >>>       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.

> > This would also return False for +/-Inf, so if the OP wants specifically
> > to check for NaN it wouldn't do. Can't think why they would, though.

> > GNAT says about "X in Long_Float'Range"
> 
> >    inf.adb:10:60: warning: explicit membership test may be optimized away

> Looks like a bug, it may not be optimized away if Long_Float is IEEE 754.

> >    inf.adb:10:60: warning: use 'Valid attribute instead
 
> Hmm, reading RM 13.9.2 makes me believe that NaN's 'Valid should yield
> True.

No, I don't think so.  3.5.7(8) says "The set of values for a floating point type is the (infinite) set of rational numbers."  This excludes NaN and infinities, which are not rational numbers (nor any kind of number).  Therefore, if X contains an infinity or NaN, X'Valid should be false.

Unlike some languages (e.g. Java), Ada doesn't define +/- Infinity or NaN to be valid values in the language, and therefore it can't make any statements about how they're supposed to be handled.  Normally, an operation that results in infinity or NaN is supposed to raise Constraint_Error.  However, the language allows for implementations not to check this, by providing a Machine_Overflows attribute that is False if Constraint_Error isn't raised.  As far as I can tell, however, the language doesn't say anything about what happens otherwise, except that the result is "unspecified" or "implementation-defined".  Even where an implementation is required to support IEEE 754 semantics (I think this is required if an implementation claims to support Annex G), I don't believe the requirements apply to NaN or Infinity even if Machine_Overflows is False--since those aren't really valid values in the language, I don't think the language can impose any requirements on how they're handled.

In particular, that means that a condition like

    if Cos_Theta /= Cos_Theta then ...

could be treated the same as

    if False then ...

by the compiler.  And yes, "X in Long_Float'Range" could be treated as "known to be True".  Maybe it will and maybe it won't, but the thing is, you can't count on it in Ada.  You may be able to count on it for a specific compiler, depending on what the compiler vendor promises.  But it won't be portable.  (The warning could be a GNAT bug within the context of what GNAT's documentation says the compiler should do--I don't know, I haven't read all of it--but this behavior is permissible in Ada.)

There are a few statements in the AARM that say "It is anticipated that an Ada binding to IEC 559:1989 will be developed in the future" or something similar, and those sections do discuss treating infinities and NaN's as values and anticipate that there will be language rules about how they're to be handled.  However, I notice that those statements were in the Ada 95 AARM and haven't really been changed since.  On Google Groups, this thread includes some posts going back to 2008 about this subject, including one by Randy about why the ARG hasn't addressed this yet.

                                   -- Adam





  reply	other threads:[~2014-05-22 15:28 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-30 10:27 How to check a Float for NaN Jerry
2008-04-30 10:47 ` christoph.grein
2008-04-30 10:50   ` christoph.grein
2008-04-30 15:02     ` Adam Beneschan
2008-04-30 20:33       ` Jerry
2008-04-30 23:23         ` Adam Beneschan
2008-05-01  1:00           ` Adam Beneschan
2008-05-01 19:52             ` Keith Thompson
2008-05-01 23:57               ` Jerry
2008-04-30 23:29       ` Randy Brukardt
2008-05-01  8:04       ` Stuart
2008-05-01 14:38         ` Adam Beneschan
2008-05-01 17:14           ` Stuart
2008-05-01 19:22             ` Randy Brukardt
2008-05-02  0:04         ` Jerry
2008-04-30 20:36 ` Jerry
2008-04-30 21:53   ` Adam Beneschan
2008-05-01  1:05     ` Jerry
2014-05-22  7:27   ` jan.de.kruyf
2014-05-22  8:09     ` Dmitry A. Kazakov
2014-05-22  9:24       ` Simon Wright
2014-05-22  9:48         ` Dmitry A. Kazakov
2014-05-22 15:28           ` Adam Beneschan [this message]
2014-05-22 16:31             ` Dmitry A. Kazakov
2014-05-22 23:33               ` Adam Beneschan
2014-05-23  7:38                 ` Dmitry A. Kazakov
2014-05-23 21:39                 ` Randy Brukardt
2014-05-27  8:35                   ` Dmitry A. Kazakov
2014-05-27 12:35                   ` Maurizio Tomasi
2014-05-27 15:53                     ` Adam Beneschan
2014-05-27 22:35                       ` Randy Brukardt
2014-05-27 22:59                         ` Jeffrey Carter
2014-05-28  7:32                         ` Dmitry A. Kazakov
2014-05-28  8:40                       ` Maurizio Tomasi
2008-05-05 18:23 ` Martin Krischik
2008-05-05 20:49   ` Adam Beneschan
2008-05-06 18:09     ` Jerry
2008-05-06 18:45       ` Wiljan Derks
2008-05-06 22:18         ` Adam Beneschan
2008-05-07 22:56           ` Randy Brukardt
2008-05-07 22:56           ` Randy Brukardt
2008-05-07 23:20             ` Adam Beneschan
2008-05-09  7:24             ` Stephen Leake
2008-05-10 17:00   ` anon
2008-05-11 22:00     ` Keith Thompson
2008-05-12  2:01       ` anon
2008-05-09 19:49 ` anon
2008-05-10  2:36   ` Jerry
2008-05-10  3:53     ` anon
2008-05-10  6:24       ` christoph.grein
2008-05-10  8:05     ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox