Problem with pow function

A

arkkimede

Hello!
I need a little help with a numerical problem about the
function pow.
In the following a simple c++ code:
----------------------------------------------start code
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <complex>
using namespace std;
main()
{
complex<long double> a;
complex<long double> b1,b2,b3,b4;
complex<long double> j;
long double x = .1L;
j = complex<long double>(0,1);
a = exp(j);
b1 = exp(j/x);
b2 = pow(b1, x);
b3 = pow(exp(j/x ), x);
b4 = a - b3;
printf("x = +0.1L\n");
printf("a = exp(j) = %+30.25lle %+30.25lle
\n",a.real(), a.imag());
printf("b1 = exp(j/x) = %+30.25lle %+30.25lle
\n",b1.real(), b1.imag());
printf("b2 = pow(b2, x) = %+30.25lle %+30.25lle \n",b2.real
(), b2.imag());
printf("b3 = pow(exp(j/x), x) = %+30.25lle %+30.25lle \n",b3.real
(), b3.imag());
printf("b4 = a - b3 = %+30.25lle %+30.25lle\n",b4.real
(), b4.imag());
}
---------------------------------stop code
My problem is that, for well know math rules, b4 could be 0 or a
number very little (10^-7).
The output are:
risultato e' il seguente:
x = +0.1L
a = exp(j) = +5.4030230586813971741400736e-01
+8.4147098480789650666459081e-01
b1 = exp(j/x) = -8.3907152907645245226058231e-01
-5.4402111088936981343191709e-01
b2 = pow(b2, x) = +9.6724905798807620156166964e-01
-2.5382919418612824645313519e-01
b3 = pow(exp(j/x), x) = +9.6724905798807620156166964e-01
-2.5382919418612824645313519e-01
b4 = a - b3 = -4.2694675211993648414766228e-01
+1.0953001789940247530635159e+00

This version of code is with long double (sizeof(long double)=12Bytes
= 96 bits) to increase precision.
In the original version there is only complex.

Could you advice me how to solve this problem ?
TIA
 
V

Victor Bazarov

arkkimede said:
Hello!
I need a little help with a numerical problem about the
function pow.
In the following a simple c++ code:
----------------------------------------------start code
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <complex>
using namespace std;
main()
{
complex<long double> a;
complex<long double> b1,b2,b3,b4;
complex<long double> j;
long double x = .1L;
j = complex<long double>(0,1);
a = exp(j);
b1 = exp(j/x);
b2 = pow(b1, x);
b3 = pow(exp(j/x ), x);
b4 = a - b3;

Did you mean to calculate

b4 = b2 - b3;

???
 
A

arkkimede

Did you mean to calculate

     b4 = b2 - b3;

???
No.
b4 = a - b3;
b4 if the power works is 0 (or a number like ~10^-7)
It looks like that the pow function for a complex number works not
correctly
 
J

Jiøí Paleèek

Hello!
I need a little help with a numerical problem about the
function pow.
In the following a simple c++ code:
----------------------------------------------start code
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <complex>
using namespace std;
main()
{
complex<long double> a;
complex<long double> b1,b2,b3,b4;
complex<long double> j;
long double x = .1L;
j = complex<long double>(0,1);
a = exp(j);
b1 = exp(j/x);
b2 = pow(b1, x);
b3 = pow(exp(j/x ), x);
b4 = a - b3;
printf("x = +0.1L\n");
printf("a = exp(j) = %+30.25lle %+30.25lle
\n",a.real(), a.imag());
printf("b1 = exp(j/x) = %+30.25lle %+30.25lle
\n",b1.real(), b1.imag());
printf("b2 = pow(b2, x) = %+30.25lle %+30.25lle \n",b2.real
(), b2.imag());
printf("b3 = pow(exp(j/x), x) = %+30.25lle %+30.25lle \n",b3.real
(), b3.imag());
printf("b4 = a - b3 = %+30.25lle %+30.25lle\n",b4.real
(), b4.imag());
}
---------------------------------stop code
My problem is that, for well know math rules, b4 could be 0 or a
number very little (10^-7).
The output are:
risultato e' il seguente:
x = +0.1L
a = exp(j) = +5.4030230586813971741400736e-01
+8.4147098480789650666459081e-01
b1 = exp(j/x) = -8.3907152907645245226058231e-01
-5.4402111088936981343191709e-01
b2 = pow(b2, x) = +9.6724905798807620156166964e-01
-2.5382919418612824645313519e-01
b3 = pow(exp(j/x), x) = +9.6724905798807620156166964e-01
-2.5382919418612824645313519e-01
b4 = a - b3 = -4.2694675211993648414766228e-01
+1.0953001789940247530635159e+00

This version of code is with long double (sizeof(long double)=12Bytes
= 96 bits) to increase precision.
In the original version there is only complex.

Could you advice me how to solve this problem ?

I think you cannot - the reason is that the "well known mathematical
rules" actually don't work that way, when we're dealing with functions of
complex variable. Look:

octave:14> log(exp(i*pi))
ans = 0.00000 + 3.14159i
octave:15> log(exp(i*pi*2))
ans = 0.0000e+00 - 2.4492e-16i
octave:16> log(exp(i*pi*3))
ans = 0.00000 + 3.14159i

The problem is that eg. the function Imag(log(exp(x*i))) = arccos(cos(i))
simply isn't identity (and cannot be).

Regards
Jiri Palecek
 
A

arkkimede

I think you cannot - the reason is that the "well known mathematical  
rules" actually don't work that way, when we're dealing with functions of  
complex variable. Look:

octave:14> log(exp(i*pi))
ans =  0.00000 + 3.14159i
octave:15> log(exp(i*pi*2))
ans = 0.0000e+00 - 2.4492e-16i
octave:16> log(exp(i*pi*3))
ans =  0.00000 + 3.14159i

The problem is that eg. the function Imag(log(exp(x*i))) = arccos(cos(i))  
simply isn't identity (and cannot be).

Regards
     Jiri Palecek

Sorry for the delay, but what do you suggest to solve this problem?
I also implemented an overloading of the function pow with same
result....
 
J

Jiøí Paleèek

.... note: this only holds in the first two quadrants, but the argument
stay intact
Sorry for the delay, but what do you suggest to solve this problem?
I also implemented an overloading of the function pow with same
result....

I'm afraid this "problem" cannot be solved in general, and certainly not
by implementing some function in C++. You must specify what exactly you
want to do with your complex numbers, so we can find a workaround -
otherwise, you have to deal with the fact that (a^b)^c==a^(b*c) only works
for natural powers (+negative) when dealing with complex numbers.

Regards
Jiri Palecek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top