Newsgroups: comp.lang.pascal
Subject: Re: Powers in Pascal
From: news@massey.ac.nz (USENET News System)
Date: Fri, 10 Apr 92 02:06:17 GMT
Organization: Massey University

This is a little after discussion seems to have ceased, but my newsreader
has been broken.

I've also lost my original copy of the post, but it has been written that
we can avoid having to use complex numbers to calculate powers of negative
numbers by the following kind of argument

     (-2)^2.2 = ((-2)^2)^1.1 = 4^1.1 = exp(1.1*ln(4)) = 4.59...

and it was claimed that this is in the FAQ.

Well, if that's the case then it must be right! :-)
Doubly so since Timo Salmi called it an interesting and imformative
article. :-) :-)

{Don't think I'm being too critical, wi orl mak mistooks. :-) }

There seems to be an assuption of 'eveness' about 2.2 although 'even' and
'odd' only really apply to TYPE INTEGER.
(What about 2.19999999999999999999999, which is represented as 2.2 in most
implementations of Pascal? Besides, you can write 2.2 as 11/5 and get
(-2)^2.2 = - 4.59... by the method given.)

In fact only integer powers of negative numbers make sense for REAL
arithmetic, COMPLEX arithmetic needs to be used in general. Therefore
such expressions will simply not arise in programs except those that use
complex numbers routinely.

Without going into a great deal of explanation about complex numbers, I
can only sketch the right answer. Those who do know about complex numbers
or who are not interested :-), can skip the rest.

A Pascal programmer would have no difficulty with the following. Define

TYPE COMPLEX = RECORD
				             Re, Im : REAL
               END;
VAR x, y : COMPLEX;

PROCEDURE ADD(x, y : COMPLEX; VAR z : COMPLEX);
BEGIN
  z.Re := x.Re + y.Re;
  z.Im := x.Im + y.Im;
END;

PROCEDURE MULT(x, y : COMPLEX; VAR z : COMPLEX);
BEGIN
  z.Re := x.Re*y.Re - x.Im*y.Im;
  z.Im := x.Re*y.Im + x.Im*y.Re;
END;

(This would be nicer if we were allowed operartors in Pascal, but that's
another story.)
A little work shows that objects of type COMPLEX with .Im = 0 behave exactly
like REAL numbers and -1 now has two square roots. .Re = 0, .Im = 1 or -1.
More work shows that there are 3 cube roots 4 fourth roots etc. If the .Re
and .Im parts are used as the coordinates of a point, then it can also be
seen that the roots are equally spaced around a circle. Thus (-2)^1.9999999
takes values which are very close together on a circle of radius 4.59....
(Since we want the 10000000 root of -2)^19999999).

To avoid this problem, mathematicians have invented the principal value which
is defined in terms of the angle to the .Re axis of the vector from 0 to the
base (using the graphical representation above).

Now I can give the rule ('At long last!', you say :-)).

                                    .Im
                                    |
                                    |       a
                                    |     /        
                                    |    /
                                    |   /
                                    |  /
                                    | /
                                    |/ u
                 -------------------|---------------- .Re
                                    |
                                    |
                                    |
                                   
If we want a^b, we find abs(a) = |a| (modulus of a) which is the length of
the vector a and arg(a) (argument of a) which is the angle u. a^b then has
modulus

            abs(a)^b and argument u*b.

Other solutions are more complicated unless 1/b is an INTEGER.

SUMMARY: Don't use REAL powers of negative numbers unless you use COMPLEX
arithmetic.

Sorry to use more bandwidth, but I couldn't let an incorrect answer go.

Terry Moore




Newsgroups: comp.lang.pascal