comp.lang.ada
 help / color / mirror / Atom feed
From: Jerry <lanceboyle@qwest.net>
Subject: Re: How to check a Float for NaN
Date: Wed, 30 Apr 2008 18:05:41 -0700 (PDT)
Date: 2008-04-30T18:05:41-07:00	[thread overview]
Message-ID: <ccd4f5f2-7556-47a3-9f55-8e6bc1ebec1f@j33g2000pri.googlegroups.com> (raw)
In-Reply-To: a5ee705a-a1f5-4466-9d8d-fbee20f99fd6@j33g2000pri.googlegroups.com

On Apr 30, 2:53 pm, Adam Beneschan <a...@irvine.com> wrote:
> On Apr 30, 1:36 pm, Jerry <lancebo...@qwest.net> wrote:
>
> > Check this out:
>
> > function Is_NaN(x : Long_Float) return Boolean is
> > begin
> >     return x /= x;
> > end Is_NaN;
>
> Maybe that will work, but I wouldn't count on it.  First of all, you
> don't know that the compiler will actually do anything.  It may get
> clever and decide that this is trivially False, and thus generate code
> that doesn't even look at x.  Second, if it generates a "floating-
> point comparison" instruction, you may not get the behavior you think
> you're getting.  My Pentium manual, for instance, says of the FCOM*
> (Compare Real) instructions: "If either operand is a NaN or is in an
> undefined format, ... the invalid-operation exception is raised, and
> the condition bits are set to 'unordered'".  Which means that (on that
> processor) the comparison attempt will probably fault, and if it
> doesn't because the fault is masked, the condition-code bits will be
> set to an "unordered" relation that your compiler may not be
> expecting, which means that it may not be translated to "inequality"
> the way you think it will.
>
> > A couple of minutes on Wikipedia saves the day. Fromhttp://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."
>
> But if you read further, you'll find that signaling NaNs "should raise
> an invalid exception".  (Why the Pentium FCOM instructions appear to
> raise exceptions for quiet NaN's as well as signed NaN's, I don't
> know.)
>
> Anyway, Wikipedia describes the IEC 559/IEEE 754 standard, but as
> we've already discussed, (1) Ada doesn't fully support this standard
> and (2) NaN's aren't valid values in Ada, so you can't reliably use
> the IEC/IEEE standard to predict what will happen if you do this in
> Ada.  You're welcome to try it, of course, but don't be surprised or
> disappointed if it doesn't work.
>
>                                  -- Adam

Adam,
Your points are well made. For "normal" use my little hack should be
considered suspect. For my trivial use of getting this pesky binding
example code out of my hair, I think it should be OK.

Just for completeness, I suppose, I'll add (as I alluded to in my
original post), that this:

    dum1 := 0.0;
    dum2 := 0.0;
    Put_Line(Long_Float'image(dum1 / dum2));

outputs this:

NaN*****************

Certainly not 754 compliance, but perhaps a bit of "awareness."
Jerry



  reply	other threads:[~2008-05-01  1:05 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 [this message]
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
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