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

**URL:** <https://community.freefem.org/t/how-can-i-calculate-the-cubic-root-of-a-negative-number/126>\
**Category:** General Discussion\
**Created:** [September 6, 2019, 3:55pm UTC](https://community.freefem.org/t/how-can-i-calculate-the-cubic-root-of-a-negative-number/126 "2019-09-06T15:55:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![glmiranda](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@glmiranda](https://community.freefem.org/u/glmiranda)\
**Post date:** [September 6, 2019, 3:55pm UTC](https://community.freefem.org/t/how-can-i-calculate-the-cubic-root-of-a-negative-number/126/1 "2019-09-06T15:55:08Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![robin997](https://avatars.discourse-cdn.com/v4/letter/r/dfb087/32.png) [@robin997](https://community.freefem.org/u/robin997)\
**Post date:** [September 6, 2019, 9:33pm UTC](https://community.freefem.org/t/how-can-i-calculate-the-cubic-root-of-a-negative-number/126/2 "2019-09-06T21:33:37Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)\
**Post date:** [September 7, 2019, 6:41am UTC](https://community.freefem.org/t/how-can-i-calculate-the-cubic-root-of-a-negative-number/126/3 "2019-09-07T06:41:19Z")

</div>

This function is directly interfaced with the [C++ one](http://www.cplusplus.com/reference/cmath/pow/). As your exponent is not an integer, it is the standard behavior.

---

<div class="post-metadata">

**Author:** ![glmiranda](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@glmiranda](https://community.freefem.org/u/glmiranda)\
**Post date:** [September 7, 2019, 11:36pm UTC](https://community.freefem.org/t/how-can-i-calculate-the-cubic-root-of-a-negative-number/126/4 "2019-09-07T23:36:41Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![glmiranda](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@glmiranda](https://community.freefem.org/u/glmiranda)\
**Post date:** [September 7, 2019, 11:38pm UTC](https://community.freefem.org/t/how-can-i-calculate-the-cubic-root-of-a-negative-number/126/5 "2019-09-07T23:38:46Z")

</div>

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!
