# How to get the cubic root for complex number?

P

#### Peng Yu

Hi,

I'm wondering how to get the cubic root for a complex number? It seems
that cbrt does not work complex numbers.

Thanks,
Peng

#include <complex>
#include <iostream>

int main () {
std::complex<double> x(2, 2);
std::cout << cbrt(x) << std::endl;
}

K

#### Kai-Uwe Bux

Peng said:
Hi,

I'm wondering how to get the cubic root for a complex number? It seems
that cbrt does not work complex numbers.

I cannot find cbrt() in the standard anyway.

Thanks,
Peng

#include <complex>
#include <iostream>

int main () {
std::complex<double> x(2, 2);
std::cout << cbrt(x) << std::endl;
}

#include <cmath>
#include <complex>
#include <iostream>
#include <ostream>

int main () {
std::complex<double> x(2, 2);
std::complex<double> r = std:ow( x, 1.0/3.0 );
std::cout << r << '\n'
<< r*r*r << '\n';
}

Best

Kai-Uwe Bux

P

#### Peng Yu

I cannot find cbrt() in the standard anyway.

cbrt() is conforming to C99.

Thanks,
Peng

D

#### dot

Jack said:
...then wait until it is added to C++, which it probably will be.
Because it is not part of the standard C++ library today.

You don't need to wait. Write yourself a function to call cbrt(),
compile under C99, link it with the rest of your program.

Ben

M

#### Michael DOUBEZ

Peng Yu a écrit :
Hi,

I'm wondering how to get the cubic root for a complex number? It seems
that cbrt does not work complex numbers.

Because it would have to give too much results.

Mathematically, for a complex number c=r*exp(i*t),r>=0, the results are
obtained by solving z**3=c=r*exp(i*(t+2*k*pi)),k natural number.

The general solution is z=r**(1/3)*exp(i*(t+2*k*pi)/3)
Relevant solutions are:

z1=r**(1/3)*exp(i*t/3)
z2=r**(1/3)*exp(i*(t+2*pi)/3)
z3=r**(1/3)*exp(i*(t-2*pi)/3)