Problem with Double.IsNan() and Double.Nan 
Author Message
 Problem with Double.IsNan() and Double.Nan

I'm trying to exit a function if a routine comes up with an invalid
number
Code as follow:

double a = -7f;
double b = 0.5;

double result = Math.Pow(a, b);

if(Double.IsNan(result)) {
    // do something

Quote:
}

If I highlight "result" in the de{*filter*} it shows up as "Nan" - but
program refuses to go into my if clause...just steps right over
it...Particularly frustrating is that if I highlight
"Double.IsNan(result)" in the if clause the VS de{*filter*} show
"true"....

I also tried "if(result == Double.Nan) "  and this didnt work
either...
any ideas ?

Many thanks

Phil



Fri, 04 Mar 2005 21:08:33 GMT  
 Problem with Double.IsNan() and Double.Nan

Quote:

> I'm trying to exit a function if a routine comes up with an invalid
> number
> Code as follow:

> double a = -7f;
> double b = 0.5;

> double result = Math.Pow(a, b);

> if(Double.IsNan(result)) {
>     // do something

> }

> If I highlight "result" in the de{*filter*} it shows up as "Nan" - but
> program refuses to go into my if clause...just steps right over
> it...Particularly frustrating is that if I highlight
> "Double.IsNan(result)" in the if clause the VS de{*filter*} show
> "true"....

> I also tried "if(result == Double.Nan) "  and this didnt work
> either...

That shouldn't work - as the documentation for Double.NaN says:
<quote>
It is not possible to determine whether a value is not a number by
comparing it to another value equal to NaN.
</quote>

Quote:
> any ideas ?

Firstly, always quote the *actual* test code you're using - the above
doesn't compile due to the method being called IsNaN rather than IsNan.
That *may* be the cause of the problem, if you're running old code due
to compilation errors. If not, it could be due to the de{*filter*} problems
that others have posted about. Try the following sample program:

using System;

public class Test
{
    public static void Main (string[] args)
    {
       double a = -7f;
        double b = 0.5;

        double result = Math.Pow(a, b);

        if(Double.IsNaN(result)) {
            Console.WriteLine ("Yes");
        }
        else
            Console.WriteLine ("No");
    }

Quote:
}

That should print out "Yes".

--

http://www.*-*-*.com/ ~skeet/
If replying to the group, please do not mail me too



Fri, 04 Mar 2005 22:57:26 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Comparing double NAN with double INF is not IEEE compliant

2. result of double*double is no double

3. warning C4756: overflow in constant arithmetic in MC++ app using Double::NaN

4. Assigning nan to a double

5. long doubles and NaN

6. Check if a float/double is NaN

7. can long double be less precise than double?

8. epsilon for float, double long double

9. Difference between double and long double?

10. Converting long double to double

11. double != double best way [Q]

12. 80bit double from mac to 64bit double to pc

 

 
Powered by phpBB® Forum Software