
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