# Board index » delphi » Need help finding root

## Need help finding root

##### Quote
>If anyone out there feels kind enough today to help me write a function
>to compute a root of a number, drop me some mail.  I'll tell you what I
>have, and maybe you can point me in the right direction.

>Thanks,
>-Larry

Are you looking for a universal nth root function?

Remember that the nth root of a number x is x^(1/n).

So all you need is a universal power function:

program root_n;

function raise(man : real; expt : real):real;
begin
raise:=(exp(expt*ln(man)));
end;

begin
writeln('The square root of 2 is ',raise(2,1/2));
writeln('The 10th root of 2344 is ',raise(2344,1/10));
end.

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

## Re:Need help finding root

If anyone out there feels kind enough today to help me write a function
to compute a root of a number, drop me some mail.  I'll tell you what I
have, and maybe you can point me in the right direction.

Thanks,
-Larry

--
"The power of accurate observation is commonly called cynicism by those
who have not got it."
-George Bernard Shaw

## Re:Need help finding root

?????? wrote on 18 Mar 96
???? about ? Need help finding root ? :

##### Quote
> If anyone out there feels kind enough today to help me write a function
> to compute a root of a number, drop me some mail.  I'll tell you what I
> have, and maybe you can point me in the right direction.

Why dont you try this:

USES Crt;
VAR a, b    :  REAL;
Result  :  REAL;

FUNCTION Root(e, n : REAL) : REAL;
BEGIN
IF (n < 0) AND (e <> 0) AND (e = Int(e)) AND
(Odd(Trunc(e)) = TRUE)
THEN Root := -(EXP(LN(ABS(n))/e))
ELSE IF (n = 0) AND (e <> 0) THEN
Root := 0
ELSE IF e = 1 THEN
Root := e
ELSE
Root := EXP(LN(n)/e);
END;

BEGIN
ClrScr;
a := 3;
b := 125;
Result := Root (a, b);  { Result=5, because 5^3=125 }
Write (Result:10:3);
END.

Greetings, Joe
?????????????????????????????????
?? j...@terra-i.rhein-ruhr.de ??
?????????????????????????????????

## Re:Need help finding root

##### Quote

:If anyone out there feels kind enough today to help me write a function
:to compute a root of a number, drop me some mail.  I'll tell you what I

A root of a number! All you need is the generic power function
covered in

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

If you need the root of a polynomial, that's much more complicated.
The late TP 4.0 numerical analysis toolbox included a good source.
You might also check (no guarantees)

303775 May 1 1991 ftp://garbo.uwasa.fi/pc/turbopas/nrpas13.zip
nrpas13.zip Numerical Recipes Pascal shareware version, 303K!

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:Need help finding root

zum Thema "Need help finding root":

##### Quote
> If anyone out there feels kind enough today to help me write a function
> to compute a root of a number, drop me some mail.  I'll tell you what I
> have, and maybe you can point me in the right direction.

The mathematical theory:

the n-th root of x you can calculate like this
exp(1/n*ln(x))
because

n  /--
-\/ x  =x^(1/n)=exp(1/n*ln(x))

So, I think this helps you to write your function.

So long
Rene
--------------------------------------------------------------------------
Fr. 5:00 Uhr bis Mo. 2:00 Uhr total Offline :-)
## CrossPoint v3.1 ##