power function: looking to improve this one.
Author Message
power function: looking to improve this one.

I have a power function here, but there is a problem with it. It returns
positive numbers, also if the exponent is odd and the base is negative: (-2)^3
returns 8 (obviously this should be -8.

This is the function.
function power(t,n: extended): extended;
begin
if t=0 then power:=0
else power := exp(n*ln(abs(t)));
end;

I changed the function to this:
function power(t,n: extended): extended;
begin
if t=0 then power:=0
else power := sign(t)*exp(n*ln(abs(t)));
end;

where sign is a function that returns the sign of t. This is wrong too, since
now the function _always_ returns negative numbers on negative bases (ie.
(-2)^2 = -4)

This is wrong too!

Erik!

_personal_ reactions preferrably by e-mail.
--
| Standard disclaimer: The views of this user are strictly his own.

Wed, 18 Jun 1902 08:00:00 GMT
power function: looking to improve this one.

Quote:
>I have a power function here, but there is a problem with it. It returns
>positive numbers, also if the exponent is odd and the base is negative: (-2)^3
>returns 8 (obviously this should be -8.

Ehhh, only when the power is an integer type.
What about (-2)^(0.5)? This is equal to Sqrt(-2), so no answer is defined,
unless you allow the result to be a complex number.
(-4)^(0.5) = 2i or -2i;
1^(1/3) = 1, or 0.5 * (1 + i*Sqrt(3)), or 0.5 * (1 + i*Sqrt(3)).

Note: This is from top of my hat, it might not be entirely correct.

Quote:
>This is the function.
>function power(t,n: extended): extended;
>begin
> if t=0 then power:=0
> else power := exp(n*ln(abs(t)));
>end;
>I changed the function to this:
>function power(t,n: extended): extended;
>begin
> if t=0 then power:=0
> else power := sign(t)*exp(n*ln(abs(t)));
>end;
>where sign is a function that returns the sign of t. This is wrong too, since
>now the function _always_ returns negative numbers on negative bases (ie.
>(-2)^2 = -4)

In your case, only multiply by Sign(t) if n is odd.

Bye,
-----------------------------------------------------------------------

Cyco Software Development
Rijswijk, Netherlands                      (+31) 70 3954179

Wed, 18 Jun 1902 08:00:00 GMT
power function: looking to improve this one.

Quote:

>I have a power function here, but there is a problem with it. It returns
>positive numbers, also if the exponent is odd and the base is negative: (-2)^3
>returns 8 (obviously this should be -8.

[SNIP]

Quote:
>I changed the function to this:
>function power(t,n: extended): extended;
>begin
> if t=0 then power:=0
> else power := sign(t)*exp(n*ln(abs(t)));
>end;

>where sign is a function that returns the sign of t. This is wrong too, since
>now the function _always_ returns negative numbers on negative bases (ie.
>(-2)^2 = -4)

Check to see if the agument is odd first then only change the sign if it is odd.  Use the
MOD operator to check this (you'll hae to test to see if it's a whole number first and
convert to integer to use the MOD operator, but this should be done anyway):

if (i MOD 2)<>0 then neg:=TRUE else neg:=FALSE;

--

The Lab BBS, Alton IL, 618-462-0767
{*filter*} the ``Communications Decency Act!''
` http://www.*-*-*.com/ ~hoxsiew' for a PGP public key.

Wed, 18 Jun 1902 08:00:00 GMT
power function: looking to improve this one.

Quote:

>I changed the function to this:
>function power(t,n: extended): extended;
>begin
> if t=0 then power:=0
> else power := sign(t)*exp(n*ln(abs(t)));
>end;
>where sign is a function that returns the sign of t. This is wrong too, since
>now the function _always_ returns negative numbers on negative bases (ie.
>(-2)^2 = -4)

Well...let's look at the algebra involved there...

x^n = n * ln(x)

(-x)^n = n * ln(-x) can not be performed though, as you may think,
because...as this is the correct means you needed to represent the
power as that you are trying to solve, since an ln cannot be negative.
You seem to realize this, but your algebraic conversion is incorrect.

Let us try something else....algebraically speaking...

(-x)^n = (-1^n)*(x^n)  (using distribution)

This is the corect method to represent what you were trying to do
algebraically....what I would like to know is how to get around the ln
requirement in this case....we got rid of that for x^n, but for
(-1)^n, it would still be exp(n*ln(-1)) which would still be a
problem.

You may need to choose another method to do this, or restrict this
function to positive powers only by setting n to integers in your
function.

Glenn Grotzinger

MOD and S3M user extraordinaire.
Writer of TP tutorial.  All released parts findable at:
ftp://garbo.uwasa.fi/pc/turbopas/tptutr11.zip

Wed, 18 Jun 1902 08:00:00 GMT
power function: looking to improve this one.

Quote:
>I have a power function here, but there is a problem with it. It returns
>positive numbers, also if the exponent is odd and the base is negative: (-2)^3
>returns 8 (obviously this should be -8.

Use the generic power function from

tsfaqp.zip Common Turbo Pascal Questions and Timo's answers

So many of the problems posted in here have already been solved for
you. Of course, on the other hand, one learns from solving anew.

All the best, Timo

....................................................................

Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa

Wed, 18 Jun 1902 08:00:00 GMT
power function: looking to improve this one.

I hope this is what you're looking for.

unit Arith;

interface

uses
SysUtils;

type
EInvalidPower = class(EMathError);

function Power(const x, y: Extended): Extended;

implementation

function Power(const x, y: Extended): Extended;
begin
if x = 0 then
Result := 0
else if x > 0 then
Result := exp(y * ln(x))
else
begin
if Trunc(y) <> y then
raise EInvalidPower.Create('Cannot raise a negative number to a
fractional power.');
Result := exp(y * ln(-x));
if (Trunc(Abs(y)) mod 2) = 1 then
Result := -Result;
end;
end;

end.

--
Colin Sarsfield
Dunham-Bush, Inc.

(540)574-6673

Wed, 18 Jun 1902 08:00:00 GMT
power function: looking to improve this one.

Quote:

>Check to see if the argument is odd first then only change the sign if
>it is odd.  Use the MOD operator to check this (you'll have to test to
>see if it's a whole number first and convert to integer to use the MOD
>operator, but this should be done anyway):

>if (i MOD 2)<>0 then neg:=TRUE else neg:=FALSE;

You mean       neg := i mod 2 <> 0 ;
or, better,    neg := Odd(i) ;

--

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages