Velocity Reviews > C++ > complex numbers

# complex numbers

Blair
Guest
Posts: n/a

 01-08-2004
could someone PLEASE tell me why this doesn't work...

-----------------------------------------
#include <complex>
using namespace std;
typedef complex<long double> cld;

void main()
{
cld cmplx, temp;
cmplx = cld(-1.0, 0.0);
temp = pow(cmplx, 0.5);
}
--------------------------------------------
the square root of -1 is j (or i) but why I debug this code temp is given as
(1,0).

or

if I do this...
void main()
{
cld cmplx, temp;
cmplx = cld(-1.0, 0.0);
temp = pow(cmplx, 0.5);
}

I get the same results. temp=(1, 0) when the real is 1 and the imaginary is
0.

So, what is wrong with the pow function? Why can't I use fractions as the
exponent? (BTW, I need to find cube-roots. That's why I'm not using
sqrt(...) )

Thanks, Blair

Dan Cernat
Guest
Posts: n/a

 01-08-2004

"Blair" <(E-Mail Removed)> wrote in message
news:3ffcb5ce_2@127.0.0.1...
> could someone PLEASE tell me why this doesn't work...
>
> -----------------------------------------
> #include <complex>
> using namespace std;
> typedef complex<long double> cld;
>
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
> --------------------------------------------
> the square root of -1 is j (or i) but why I debug this code temp is given

as
> (1,0).
>
> or
>
> if I do this...
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
>
> I get the same results. temp=(1, 0) when the real is 1 and the imaginary

is
> 0.
>
> So, what is wrong with the pow function? Why can't I use fractions as the
> exponent? (BTW, I need to find cube-roots. That's why I'm not using
> sqrt(...) )
>
>
> Thanks, Blair
>
>

my first guess is that pow function (as is declared in <cmath>) gets the
first argument converted to a double or float.

dan

Cy Edmunds
Guest
Posts: n/a

 01-08-2004
"Blair" <(E-Mail Removed)> wrote in message
news:3ffcb5ce_2@127.0.0.1...
> could someone PLEASE tell me why this doesn't work...
>
> -----------------------------------------
> #include <complex>
> using namespace std;
> typedef complex<long double> cld;
>
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
> --------------------------------------------
> the square root of -1 is j (or i) but why I debug this code temp is given

as
> (1,0).
>
> or
>
> if I do this...
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
>
> I get the same results. temp=(1, 0) when the real is 1 and the imaginary

is
> 0.
>
> So, what is wrong with the pow function? Why can't I use fractions as the
> exponent? (BTW, I need to find cube-roots. That's why I'm not using
> sqrt(...) )
>
>
> Thanks, Blair
>
>

Try

temp = pow(cmplx, 0.5L);

--
Cy
http://home.rochester.rr.com/cyhome/

Victor Bazarov
Guest
Posts: n/a

 01-08-2004
"Dan Cernat" <(E-Mail Removed)> wrote...
>
> "Blair" <(E-Mail Removed)> wrote in message
> news:3ffcb5ce_2@127.0.0.1...
> > could someone PLEASE tell me why this doesn't work...
> >
> > -----------------------------------------
> > #include <complex>
> > using namespace std;
> > typedef complex<long double> cld;
> >
> > void main()
> > {
> > cld cmplx, temp;
> > cmplx = cld(-1.0, 0.0);
> > temp = pow(cmplx, 0.5);
> > }
> > --------------------------------------------
> > the square root of -1 is j (or i) but why I debug this code temp is

given
> as
> > (1,0).
> >
> > or
> >
> > if I do this...
> > void main()
> > {
> > cld cmplx, temp;
> > cmplx = cld(-1.0, 0.0);
> > temp = pow(cmplx, 0.5);
> > }
> >
> > I get the same results. temp=(1, 0) when the real is 1 and the imaginary

> is
> > 0.
> >
> > So, what is wrong with the pow function? Why can't I use fractions as

the
> > exponent? (BTW, I need to find cube-roots. That's why I'm not using
> > sqrt(...) )
> >
> >
> > Thanks, Blair
> >
> >

>
> my first guess is that pow function (as is declared in <cmath>) gets the
> first argument converted to a double or float.

<cmath> is not included and <complex> declares its own 'std:ow' (and
actually the whole four of them).

Got a second guess?

My first guess is that 'void main' causes it.

Just kidding. Actually, as I found by trying, changing it to

temp = pow(cmplx, cld(0.5, 0));

gives the right result. Why it happens, I can't say, it's too late
here for me to strain my brain :-[

Victor

CrayzeeWulf
Guest
Posts: n/a

 01-08-2004
Blair wrote:

> could someone PLEASE tell me why this doesn't work...
>
> -----------------------------------------
> #include <complex>
> using namespace std;
> typedef complex<long double> cld;
>
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
> --------------------------------------------
> the square root of -1 is j (or i) but why I debug this code temp is given
> as (1,0).
>

Hi Blair,

There are four different versions of std:ow for complex<T> numbers:

1. template<class T>
complex<T> pow( const complex<T>& x, int y) ;

2. template<class T>
complex<T> pow( const complex<T>& x, const complex<T>& y) ;

3. template<class T>
complex<T> pow( const complex<T>& x, const T& y) ;

4. template<class T>
complext<T> pow( const T& x, const complex<T>& y ) ;

In your case "T" is "long double". Hence, the call "pow(cmplx, 0.5)" does
not directly match any of the above without requiring some implicit
conversion. Overload resolution rules end up matching the call to prototype
#1 in the above list when you really want to use #3. In order to match #3,
you have to help the compiler a little bit by telling it that "0.5" in your
function call is indeed a "long double":

temp = pow( cmplx, 0.5L ) ;

Another option is to use prototype #2 by converting "0.5" into a
complex<long double>:

temp = pow( cmplx, cld(0.5, 0.0) ) ;

> if I do this...
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
>

I do not see the difference between this and the version above.

Later,
--
CrayzeeWulf

Blair
Guest
Posts: n/a

 01-08-2004
Cy and Victor,
Thanks for the replies. They both worked for a real result case - ie. when
cmplx=cld(4,0) then temp becomes 2. But neither answer works for imaginary
results.

PS. I made a mistake in my original post ... I wrote
--------------------------------
....or

if I do this...
void main()
{
cld cmplx, temp;
cmplx = cld(-1.0, 0.0);
temp = pow(cmplx, 0.5);
}
-----------------------------------
but that code is exactly the same as the first snippet of code that posted!
I mean to say this...
--------------------------------
or

if I do this...
void main()
{
cld cmplx, temp;
cmplx = cld(4.0, 0.0);
temp = pow(cmplx, 0.5);
}
-----------------------------------
to make cmplx = 4.0 + 0j
then temp should = 2.0, but I was getting temp=1+0j. Now, both of your
answers correct the case when sqrt(cmplx) is a real number. But it still
fails when cmplx = -1. (which, of course, should make temp=0+j)

Thanks,
blair

"Blair" <(E-Mail Removed)> wrote in message
news:3ffcb5ce_2@127.0.0.1...
> could someone PLEASE tell me why this doesn't work...
>
> -----------------------------------------
> #include <complex>
> using namespace std;
> typedef complex<long double> cld;
>
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
> --------------------------------------------
> the square root of -1 is j (or i) but why I debug this code temp is given

as
> (1,0).
>
> or
>
> if I do this...
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
>
> I get the same results. temp=(1, 0) when the real is 1 and the imaginary

is
> 0.
>
> So, what is wrong with the pow function? Why can't I use fractions as the
> exponent? (BTW, I need to find cube-roots. That's why I'm not using
> sqrt(...) )
>
>
> Thanks, Blair
>
>

Blair
Guest
Posts: n/a

 01-08-2004
"CrayzeeWulf" <(E-Mail Removed)> wrote in message
news:MG3Lb.59459\$(E-Mail Removed)...
> Blair wrote:
>
> > could someone PLEASE tell me why this doesn't work...
> >
> > -----------------------------------------
> > #include <complex>
> > using namespace std;
> > typedef complex<long double> cld;
> >
> > void main()
> > {
> > cld cmplx, temp;
> > cmplx = cld(-1.0, 0.0);
> > temp = pow(cmplx, 0.5);
> > }
> > --------------------------------------------
> > the square root of -1 is j (or i) but why I debug this code temp is

given
> > as (1,0).
> >

> Hi Blair,
>
> There are four different versions of std:ow for complex<T> numbers:
>
> 1. template<class T>
> complex<T> pow( const complex<T>& x, int y) ;
>
> 2. template<class T>
> complex<T> pow( const complex<T>& x, const complex<T>& y) ;
>
> 3. template<class T>
> complex<T> pow( const complex<T>& x, const T& y) ;
>
> 4. template<class T>
> complext<T> pow( const T& x, const complex<T>& y ) ;
>
> In your case "T" is "long double". Hence, the call "pow(cmplx, 0.5)" does
> not directly match any of the above without requiring some implicit
> conversion. Overload resolution rules end up matching the call to

prototype
> #1 in the above list when you really want to use #3. In order to match #3,
> you have to help the compiler a little bit by telling it that "0.5" in

your
> function call is indeed a "long double":
>
> temp = pow( cmplx, 0.5L ) ;
>
> Another option is to use prototype #2 by converting "0.5" into a
> complex<long double>:
>
> temp = pow( cmplx, cld(0.5, 0.0) ) ;
>

Hi, and thanks for the response. But, ...darn it... both suggestions were
already posted and neither of them seemed to work for me for some reason. In
fact, if I instead do this...

void main()
{
cld cmplx, temp;
cmplx = cld(-1.0, 0.0);
temp = pow(cmplx, 0.5);
}

I get the result: temp = (-1.#IND000000000, 0.00000000000000). HOWEVER, if I
make the imaginary part of cmplx NON-ZERO, as in...

typedef complex<long double> cld;
void main()
{
cld cmplx, temp;
cmplx = cld(-1.0, 1.0);
temp = pow(cmplx, 0.5L);
}

I do get the correct answer: temp = (0.45508986056223, 1.098684113467!!!

Strange, huh?

Blair

>
> > if I do this...
> > void main()
> > {
> > cld cmplx, temp;
> > cmplx = cld(-1.0, 0.0);
> > temp = pow(cmplx, 0.5);
> > }
> >

>
> I do not see the difference between this and the version above.

Yeah, sorry about that. I noticed that too. I posted a correction.

>
> Later,
> --
> CrayzeeWulf

Cy Edmunds
Guest
Posts: n/a

 01-08-2004
[snip]

Here is my test program.

void yowza()
{
std::complex<long double> cmplx(-1.0, 0.0), temp;
temp = std:ow(cmplx, 0.5L);
std::cout << temp << '\n';
}

Output is:

(6.12303e-017,1)

Compiler is VC++.Nuts

--
Cy
http://home.rochester.rr.com/cyhome/

Blair
Guest
Posts: n/a

 01-08-2004
Crap, I messed up again... see correction below...

"Blair" <(E-Mail Removed)> wrote in message
news:3ffcce01_2@127.0.0.1...
> "CrayzeeWulf" <(E-Mail Removed)> wrote in message
> news:MG3Lb.59459\$(E-Mail Removed)...
> > Blair wrote:
> >
> > > could someone PLEASE tell me why this doesn't work...
> > >
> > > -----------------------------------------
> > > #include <complex>
> > > using namespace std;
> > > typedef complex<long double> cld;
> > >
> > > void main()
> > > {
> > > cld cmplx, temp;
> > > cmplx = cld(-1.0, 0.0);
> > > temp = pow(cmplx, 0.5);
> > > }
> > > --------------------------------------------
> > > the square root of -1 is j (or i) but why I debug this code temp is

> given
> > > as (1,0).
> > >

> > Hi Blair,
> >
> > There are four different versions of std:ow for complex<T> numbers:
> >
> > 1. template<class T>
> > complex<T> pow( const complex<T>& x, int y) ;
> >
> > 2. template<class T>
> > complex<T> pow( const complex<T>& x, const complex<T>& y) ;
> >
> > 3. template<class T>
> > complex<T> pow( const complex<T>& x, const T& y) ;
> >
> > 4. template<class T>
> > complext<T> pow( const T& x, const complex<T>& y ) ;
> >
> > In your case "T" is "long double". Hence, the call "pow(cmplx, 0.5)"

does
> > not directly match any of the above without requiring some implicit
> > conversion. Overload resolution rules end up matching the call to

> prototype
> > #1 in the above list when you really want to use #3. In order to match

#3,
> > you have to help the compiler a little bit by telling it that "0.5" in

> your
> > function call is indeed a "long double":
> >
> > temp = pow( cmplx, 0.5L ) ;
> >
> > Another option is to use prototype #2 by converting "0.5" into a
> > complex<long double>:
> >
> > temp = pow( cmplx, cld(0.5, 0.0) ) ;
> >

>
> Hi, and thanks for the response. But, ...darn it... both suggestions were
> already posted and neither of them seemed to work for me for some reason.

In
> fact, if I instead do this...
>

------insert the following line-------
typedef complex<double> cld; //i.e. I changed my "long double" to "double"
------------------------------------
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 0.0);
> temp = pow(cmplx, 0.5);
> }
>
> I get the result: temp = (-1.#IND000000000, 0.00000000000000). HOWEVER, if

I
> make the imaginary part of cmplx NON-ZERO, as in...
>
> typedef complex<long double> cld;
> void main()
> {
> cld cmplx, temp;
> cmplx = cld(-1.0, 1.0);
> temp = pow(cmplx, 0.5L);
> }
>
> I do get the correct answer: temp = (0.45508986056223, 1.098684113467!!!
>
> Strange, huh?
>
> Blair
>
> >
> > > if I do this...
> > > void main()
> > > {
> > > cld cmplx, temp;
> > > cmplx = cld(-1.0, 0.0);
> > > temp = pow(cmplx, 0.5);
> > > }
> > >

> >
> > I do not see the difference between this and the version above.

>
> Yeah, sorry about that. I noticed that too. I posted a correction.
>
> >
> > Later,
> > --
> > CrayzeeWulf

>
>

Blair
Guest
Posts: n/a

 01-08-2004
So, it is a compiler issue. I get:

Output: (-1.#IND,0)

Thanks for the help. I'd better investigate the compiler, I guess.

Blair

"Cy Edmunds" <(E-Mail Removed)> wrote in message
news:X74Lb.41274\$(E-Mail Removed)...
> [snip]
>
> Here is my test program.
>
> void yowza()
> {
> std::complex<long double> cmplx(-1.0, 0.0), temp;
> temp = std:ow(cmplx, 0.5L);
> std::cout << temp << '\n';
> }
>
> Output is:
>
> (6.12303e-017,1)
>
> Compiler is VC++.Nuts
>
> --
> Cy
> http://home.rochester.rr.com/cyhome/
>
>

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Kottiyath Python 22 03-28-2009 10:11 PM Robert Mark Bram Java 0 02-04-2007 10:06 AM oulan bator Java 29 12-04-2005 12:51 PM Stanimir Stamenkov XML 2 10-25-2005 10:16 AM news.amnet.net.au Java 1 04-13-2004 07:10 AM