How can I calculate the cubic root of a negative number?

Hi!!
I am obtaining a “nan” every time that x takes a negative value in pow(x,1./3.)?

What am I doing wrong?

Thank you!!!

@glmiranda I think that is the definition of the power function. You can do the root of the absolute value, and times it with the sign function.

This function is directly interfaced with the C++ one. As your exponent is not an integer, it is the standard behavior.

Hi Simon!
You know, in c++ there exists the “float cbrt( float )” function which can handle negative arguments but this kind of function doesn’t exist in Freefem. This is strange because Freefem was created from c++, I think …

Any way, in my program, I had to define my own version as follows:
func real cbcrt( real z )
{
if(z<0)
return -pow(-z,1./3.);
else
return pow(z,1./3.);
}
And it seems that works!

Thank you for answering my question!!

Hi Robin!

Yeah, you are right:

func real cbcrt( real z )
{
if(z<0)
return -pow(-z,1./3.);
else
return pow(z,1./3.);
}

Thank you!