[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

How to get the cubic root for complex number?

Peng Yu

9/10/2008 1:00:00 AM

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;
}
6 Answers

Kai-Uwe Bux

9/10/2008 1:11:00 AM

0

Peng Yu wrote:

> 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::pow( x, 1.0/3.0 );
std::cout << r << '\n'
<< r*r*r << '\n';
}


Best

Kai-Uwe Bux

Peng Yu

9/10/2008 1:21:00 AM

0

On Sep 9, 8:10 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Peng Yu wrote:
> > 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.

cbrt() is conforming to C99.

Thanks,
Peng

Alf P. Steinbach

9/10/2008 1:26:00 AM

0

* Peng Yu:
>
> 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;
> }

cbrt is a C99 function, not guaranteed to be present in current standard C++,
and anyway you'd need to include <math.h> and to use that you'd have to express
the complex number in polar form and use it on the magnitude part (and divide
angle by 3).

Just use std::pow.


Cheers & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jack Klein

9/10/2008 3:46:00 AM

0

On Tue, 9 Sep 2008 18:20:45 -0700 (PDT), Peng Yu <PengYu.UT@gmail.com>
wrote in comp.lang.c++:

> On Sep 9, 8:10 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> > Peng Yu wrote:
> > > 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.
>
> cbrt() is conforming to C99.

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

--
Jack Klein
Home: http://JK-Tech...
FAQs for
comp.lang.c http://...
comp.lang.c++ http://www.parashift.com/c++...
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-...

benhonghgmailcom

9/10/2008 5:55:00 AM

0

Jack Klein wrote:
> On Tue, 9 Sep 2008 18:20:45 -0700 (PDT), Peng Yu <PengYu.UT@gmail.com>
> wrote in comp.lang.c++:
>
>> On Sep 9, 8:10 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
>>> Peng Yu wrote:
>>>> 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.
>> cbrt() is conforming to C99.
>
> ...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

Michael DOUBEZ

9/10/2008 8:45:00 AM

0

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)

--
Michael