I relatively new to C# and since Math.Pow(x,y) returns NaN for a negative number(read: not negative power), I want to know how to calculate result more efficiently. What I am do
in Pow(double base, double power), when base < 0 and power is fractional ( ex: power=0.5) , then NaN will be generated, because it is calculable in complex domain not in real domain. so may be you need to check the base and power of pow function before using them. in this case you should return your desired value as undefined value (instead of NaN).
the bellow function performs this operation, and returns 0 instead of NaN:
// pow with desired Nan
public double Pow(double x,double y){
double MyNaN = 0; // or any desired value for Nan
double result = MyNaN;
if (Math.Floor (y) != y) { // if y is fractional number
if (x < 0) // if x is negative number
return result;
}
result = Math.Pow (x, y);
return result;
}