Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with pow function

2 views
Skip to first unread message

arkkimede

unread,
Sep 25, 2009, 9:40:13 AM9/25/09
to
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

Victor Bazarov

unread,
Sep 25, 2009, 9:58:57 AM9/25/09
to
arkkimede wrote:
> 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;

???


--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

arkkimede

unread,
Sep 25, 2009, 10:04:32 AM9/25/09
to
On Sep 25, 3:58 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> 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

Jiří Paleček

unread,
Sep 25, 2009, 10:24:54 AM9/25/09
to 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

arkkimede

unread,
Sep 28, 2009, 4:40:06 AM9/28/09
to
On 25 Set, 16:24, Jiří Paleček <jpale...@web.de> wrote:
> 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....

Jiří Paleček

unread,
Sep 29, 2009, 11:14:55 AM9/29/09
to arkkimede
On Mon, 28 Sep 2009 10:40:06 +0200, arkkimede <arkk...@gmail.com> wrote:

> On 25 Set, 16:24, Ji�� Pale�ek <jpale...@web.de> wrote:
>> 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)) ďż˝

... note: this only holds in the first two quadrants, but the argument
stay intact

>> simply isn't identity (and cannot be).
>

> 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

0 new messages