Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Ada > Re: How to chec...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 13 of 35 Topic 5676 of 5800
Post > Topic >>

Re: How to check a Float for NaN

by Jerry <lanceboyle@[EMAIL PROTECTED] > Apr 30, 2008 at 06:05 PM

On Apr 30, 2:53=A0pm, Adam Beneschan <a...@[EMAIL PROTECTED]
> wrote:
> On Apr 30, 1:36 pm, Jerry <lancebo...@[EMAIL PROTECTED]
> wrote:
>
> > Check this out:
>
> > function Is_NaN(x : Long_Float) return Boolean is
> > begin
> > =A0 =A0 return x /=3D x;
> > end Is_NaN;
>
> Maybe that will work, but I wouldn't count on it. =A0First of all, you
> don't know that the compiler will actually do anything. =A0It may get
> clever and decide that this is trivially False, and thus generate code
> that doesn't even look at x. =A0Second, if it generates a "floating-
> point comparison" instruction, you may not get the behavior you think
> you're getting. =A0My 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'". =A0Which 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". =A0(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 sup****t 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. =A0You're welcome to try it, of course, but don't be surprised or
> disappointed if it doesn't work.
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0--
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 :=3D 0.0;
    dum2 :=3D 0.0;
    Put_Line(Long_Float'image(dum1 / dum2));

outputs this:

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

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




 35 Posts in Topic:
How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-04-30 03:27:51 
Re: How to check a Float for NaN
christoph.grein@[EMAIL PR  2008-04-30 03:47:54 
Re: How to check a Float for NaN
christoph.grein@[EMAIL PR  2008-04-30 03:50:15 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-04-30 08:02:05 
Re: How to check a Float for NaN
"Randy Brukardt"  2008-04-30 18:29:14 
Re: How to check a Float for NaN
"Stuart" <st  2008-05-01 09:04:11 
Re: How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-04-30 13:33:17 
Re: How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-04-30 13:36:21 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-04-30 14:53:54 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-04-30 16:23:13 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-04-30 18:00:21 
Re: How to check a Float for NaN
Keith Thompson <kst-u@  2008-05-01 12:52:16 
Re: How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-04-30 18:05:41 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-05-01 07:38:40 
Re: How to check a Float for NaN
"Stuart" <st  2008-05-01 18:14:09 
Re: How to check a Float for NaN
"Randy Brukardt"  2008-05-01 14:22:35 
Re: How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-05-01 16:57:26 
Re: How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-05-01 17:04:53 
Re: How to check a Float for NaN
Martin Krischik <krisc  2008-05-05 20:23:02 
Re: How to check a Float for NaN
anon@[EMAIL PROTECTED] (  2008-05-10 17:00:51 
Re: How to check a Float for NaN
Keith Thompson <kst-u@  2008-05-11 15:00:55 
Re: How to check a Float for NaN
anon@[EMAIL PROTECTED] (  2008-05-12 02:01:18 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-05-05 13:49:25 
Re: How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-05-06 11:09:56 
Re: How to check a Float for NaN
"Wiljan Derks"   2008-05-06 20:45:47 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-05-06 15:18:39 
Re: How to check a Float for NaN
"Randy Brukardt"  2008-05-07 17:56:57 
Re: How to check a Float for NaN
Stephen Leake <Stephe.  2008-05-09 03:24:21 
Re: How to check a Float for NaN
"Randy Brukardt"  2008-05-07 17:56:57 
Re: How to check a Float for NaN
Adam Beneschan <adam@[  2008-05-07 16:20:55 
Re: How to check a Float for NaN
anon@[EMAIL PROTECTED] (  2008-05-09 19:49:50 
Re: How to check a Float for NaN
Jerry <lanceboyle@[EMA  2008-05-09 19:36:58 
Re: How to check a Float for NaN
anon@[EMAIL PROTECTED] (  2008-05-10 03:53:54 
Re: How to check a Float for NaN
Georg Bauhaus <see.rep  2008-05-10 10:05:33 
Re: How to check a Float for NaN
christoph.grein@[EMAIL PR  2008-05-09 23:24:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Jul 6 19:48:10 CDT 2008.