tags:

views:

88

answers:

1

So there is a method for NaN, but divide by zero creates infinity or negative infinity.

There is a method for Infinity (also positive infinite and negative infinity).

What I want is IsARealNumber function that returns true when the value is an expressible number.

Obviously I can write my own...

public bool IsARealNumber(double test)
{
    if (double.IsNaN(test)) return false;
    if (double.IsInfinity(test)) return false;
    return true;
}

but it doesn't seem like I should have to.

+4  A: 

To add it as an extension method, it has to be a static member of a static class.

public static class ExtensionMethods
{
    public static bool IsARealNumber(this double test)
    {
        return !double.IsNaN(test) && !double.IsInfinity(test);
    }
}
GalacticCowboy
awesome, thanks.
Joel Barsotti
Am I right in assuming that you can only add these ExtensionMethods to objects, and not hang them off the class proper. Like double.IsNaN(double) is?
Joel Barsotti
I believe that is correct. It uses the type of the "this" parameter to determine what it applies to, and I doubt it's allowed to be static (so as to hang it off the class as a static member).
GalacticCowboy