# Board index » delphi » Re : How to raise a value to a power - not that simple!

## Re : How to raise a value to a power - not that simple!

##### Quote
r...@valis.worldgate.edmonton.ab.ca (Ryan Shook) wrote:

>This may sound trivial, but how do you raise a value to a power?
>   eg). 2^12 = 4095

>I CANNOT find a function or operator to handle this simple mathematical
>function. Any help would be appreciated.

>Ryan

The question is not that trivial. The trouble is that the power function
is not that simple. Several distinct situations need to be considered for
the function Power(X, N) i.e X^N.

1) X don't care, N = 0
2) X = 1, N don't care
3) X = 0 and N > 0
4) X = 0 and N < 0
5) X > 0
6) X < 0 and N is an odd integer
7) X < 0 and N is an even integer
8) X < 0 and N not an integer

Consider the following robust (though not necessarily the most
efficient!) power function.

interface

type
EPowerException = class(Exception)
end;

implementation

function Power(X, N : real) : extended;
var
t : longint;
r : real;
isInteger : boolean;
begin

if N = 0 then begin
result := 1.0;
exit;
end;

if X = 1.0 then begin
result := 1.0;
exit;
end;

if X = 0.0 then begin
if N > 0.0 then
begin
result := 0.0;
exit;
end
else
raise EPowerException.Create('Infinite Result');
end;

if (X > 0) then
try
result := exp(N * ln(X));
exit;
except
raise EPowerException.Create('Overflow/Underflow Result');
end;

{ X is negative but we still may compute the result if n is an integer}
{ try and get integer portion of n into a longint, it will be quicker to

##### Quote
}       { compute odd n}

try
t := trunc(n);
if (n - t) = 0 then
isInteger := true
else
isInteger := False;
except
{Bit superfluous as result will probably underflow/overflow anyway}
r := int(n);
if (n - r) = 0 then
begin
isInteger := true;
if frac(r/2) = 0.5 then
t := 1
else
t := 2;
end
else
isInteger := False;
end;

if isInteger then
begin
{n is an integer}
if odd(t) then
{n is odd}
try
result := -exp(N * ln(-X));
exit;
except
raise EPowerException.Create('Overflow/Underflow Result');
end
else
{n is even}
try
result := exp(N * ln(-X));
exit;
except
raise EPowerException.Create('Overflow/Underflow Result');
end;
end
else
raise EPowerException.Create('Complex Result');

end;

Paul Cunningham
pjcunning...@cix.compulink.co.uk

## Re:Re : How to raise a value to a power - not that simple!

In article <DHIr27....@cix.compulink.co.uk>,
pjcunning...@cix.compulink.co.uk ("Paul Cunningham") wrote:

##### Quote
>r...@valis.worldgate.edmonton.ab.ca (Ryan Shook) wrote:

>>This may sound trivial, but how do you raise a value to a power?
>>   eg). 2^12 = 4095

>>I CANNOT find a function or operator to handle this simple mathematical
>>function. Any help would be appreciated.

>>Ryan

>The question is not that trivial. The trouble is that the power function
>is not that simple. Several distinct situations need to be considered for
>the function Power(X, N) i.e X^N.

[Snipped long involved power functions]

I'm not exactly a mathematician, but I rearranged the definition of logs to
come up with the following Power function that seems to work quite well for
me.

{--------------------------------------}
function Power(a,x:Real):real;

begin
if a = 0 then
Power := 1
else
Power := exp(x*ln(abs(a)));
end;

{--------------------------------------}
Function IntPower(InVal:integer; Exponent:integer):integer;

begin
IntPower := Round(Power(InVal,Exponent));
end;  { Int Power }

Mike

[]== Michael Lang ===============[][]=================== ml...@america.net ==[]
\|  President                     \/  "What a good thing Adam had - when     |/
|  Argus Computing, Inc.         ||   he said a good thing, he knew nobody  |
/|  V/Fax (770) 926-3555          /\   had said it before."   --Mark Twain   |\
[]===============================[][]=http://america.net/~mlang/mlang.html ==[]