Board index » delphi » power function: looking to improve this one.

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.

 

Re:power function: looking to improve this one.


In article <544_9604240...@idn.nl>,
Er...@hcc-gron.idn.nl (Erik Huelsmann) says:

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.
When you allow such answers, remember you get multiple answers, eg.:
  (-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,
-----------------------------------------------------------------------
  Zweitze de Vries                           zwei...@cyco.nl
  Cyco Software Development
  Handelskade 49
  Rijswijk, Netherlands                      (+31) 70 3954179

Re:power function: looking to improve this one.


In <544_9604240...@idn.nl>, Er...@hcc-gron.idn.nl (Erik Huelsmann) writes:

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;

--
Wayne D. Hoxsie Jr.  Team OS/2   <hoxs...@ezl.com>
The Lab BBS, Alton IL, 618-462-0767
{*word*30} the ``Communications Decency Act!''
`http://www.ezl.com/~hoxsiew' for a PGP public key.

Re:power function: looking to improve this one.


Quote
Er...@hcc-gron.idn.nl (Erik Huelsmann) wrote:
>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
mailto:ggr...@2sprint.net
MOD and S3M user extraordinaire.
Writer of TP tutorial.  All released parts findable at:
ftp://garbo.uwasa.fi/pc/turbopas/tptutr11.zip

Re:power function: looking to improve this one.


In article <4lo5h5$...@elektron.et.tudelft.nl>,
Zweitze de Vries <zwei...@cyco.nl> wrote:
In article <544_9604240...@idn.nl>,
Er...@hcc-gron.idn.nl (Erik Huelsmann) says:

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

 96535 Mar 30 20:19 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 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

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
t...@uwasa.fi http://uwasa.fi/~ts BBS 961-3170972; FIN-65101,  Finland

Re: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.
col...@shentel.net
(540)574-6673

Re:power function: looking to improve this one.


In article <changi0.9m.29ZJht$...@nntp.hoxsiew.ezl.com> in

Quote
comp.lang.pascal.borland, hoxs...@ezl.com wrote:
>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) ;

--
John Stockton, Surrey, UK.  J...@merlyn.demon.co.uk  Turnpike v1.12  MIME

Other Threads