private double f(double x, double zn = 1)
{
double X = - zn;
X *= x * x * (x + 1);
X *= Math.Pow((x - 2), 0.333);
return funct ? x : X;
}
I have this code. When I try to find Math.Pow((x-2), 0.333) – i have NaN.
How to solve it? Why NaN?
Rewritten…
private double f(double x, double zn = 1)
{
double answer = - zn;
answer *= x * x * (x + 1);
answer *= Math.Pow((x - 2), 0.333);
return answer;
}
My guess is that you’re taking the cube root of a negative number. That seems the most likely cause, but your code is really hard to read due to having both
xandXas local variables…After closer examination, as you’re not actually modifying
xat any point, it really depends on the incoming value ofx. If it’s a finite value greater than or equal to 2, it should be fine. But ifxis smaller than 2, it will fail (well, return NaN) for reasons of simple maths…