# Board index » delphi » Help with a recursive function needed...

## Help with a recursive function needed...

Hi,
Can someone tell me if there is a fundamental error in the following
recursive function. When i call it i sometimes get the correct return, but
often i get an "Invalid Floating point operation" exception. Can someone
help me before i go totally nuts...

function recurse(var  t,e: extended; var count : integer): extended;
var x : extended;
begin
count:=count+1;
if (count<100)then
begin
x:=e*sin(recurse(t,e, count));
recurse:= pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
end;
end;

Best regards

Magnus
mag...@leijonborg.se

## Re:Help with a recursive function needed...

You do not have a result if the function does not recurse.  In other words,
what is the return value when count equals 100?  It is undefined because you
do not set the return value to anything.

## Re:Help with a recursive function needed...

OK, What should it look like? I'm totally lost. I always call the function
with count set to 1.

/Magnus

##### Quote
"Gabriel Gonzalez" <no-s...@no-spam.com> wrote in message

news:rdGcnYHxmsv3u-ujXTWchA@giganews.com...
##### Quote
> You do not have a result if the function does not recurse.  In other
words,
> what is the return value when count equals 100?  It is undefined because
you
> do not set the return value to anything.

## Re:Help with a recursive function needed...

##### Quote
> OK, What should it look like? I'm totally lost. I always call the function
> with count set to 1.

Who wrote the function?  Aske them what the value of the function should be
when count=100.  Right now it is undefined, and since I have no idea of what
the function represents, I cannot just make up a value.

Here's an undated version:

function recurse(var  t,e: extended; var count : integer): extended;
var x : extended;
begin
count:=count+1;
if (count<100)then
begin
x:=e*sin(recurse(t,e, count));
recurse:= pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
end else begin
recurse := ???????;
end;
end;

Replace the question marks with whatever is supposed to go there.

Also:  As it is, the output of your function is meaningless.  Since the
return value is not defined for the deepest recursion level, it is returning
ramdom data.  Since the deepest recusion level affects the values of all the
other recursion levels, then the output of the function as a whole is random
and worthless.  I really recommeend that you ask the original writer what
the ?????? value should be.

##### Quote

> /Magnus

> "Gabriel Gonzalez" <no-s...@no-spam.com> wrote in message
> news:rdGcnYHxmsv3u-ujXTWchA@giganews.com...
> > You do not have a result if the function does not recurse.  In other
> words,
> > what is the return value when count equals 100?  It is undefined because
> you
> > do not set the return value to anything.

## Re:Help with a recursive function needed...

##### Quote
> Here's an undated version:

In case you're wondering, the should say "updated"... :-)

## Re:Help with a recursive function needed...

The problem is that it is giving me correct data back, that is most of the
time. ;\ I wrote it myself and i get your point. I have been looking at the
result and are rewriting it, but so far i get the same exception anyway.

/M

##### Quote
"Gabriel Gonzalez" <no-s...@no-spam.com> wrote in message

news:tBmcnfdvo7vdr-ujXTWciQ@giganews.com...
##### Quote
> > Here's an undated version:

> In case you're wondering, the should say "updated"... :-)

## Re:Help with a recursive function needed...

##### Quote
> The problem is that it is giving me correct data back, that is most of the
> time. ;\ I wrote it myself and i get your point. I have been looking at
the
> result and are rewriting it, but so far i get the same exception anyway.

If you get the correct result most of the time, then ???? should _probably_
be zero.

Look, at this stage, the problem is easy:  What should the value be at
count=100?  count=100 is when the function stops recursing.  Put that value
into where I put the ???? and you should be further ahead.

## Re:Help with a recursive function needed...

On Mon, 17 Mar 2003 19:21:32 GMT, "Magnus L" <mag...@leijonborg.se>
wrote:

##### Quote
>Can someone tell me if there is a fundamental error in the following
>recursive function.

One question - does it need to be recursive?

## Re:Help with a recursive function needed...

Yes, i need to do at least ten iterations to get a value that is precise
enough.

/Magnus

##### Quote
"Jan Philips" <judmccrNOSP...@bellsouth.net> wrote in message

news:tsgc7vc60epud6ku6ct7pl74mclkmmkjkc@4ax.com...
##### Quote
> On Mon, 17 Mar 2003 19:21:32 GMT, "Magnus L" <mag...@leijonborg.se>
> wrote:

> >Can someone tell me if there is a fundamental error in the following
> >recursive function.

> One question - does it need to be recursive?

## Re:Help with a recursive function needed...

In article <CEWdnUdHIJhys-ujXTW...@giganews.com>, "Gabriel Gonzalez"

##### Quote
<no-s...@no-spam.com> writes:
>function recurse(var  t,e: extended; var count : integer): extended;
>var x : extended;
>begin
>  count:=count+1;
>  if (count<100)then
>  begin
>     x:=e*sin(recurse(t,e, count));
>     recurse:= pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
>  end else begin
>    recurse := ???????;
>  end;
>end;

Surely this is no different from ...

var
Value, t, e, x : extended;
i : integer;
begin
t := ???;
e := ???;
Value := ????; // starting value
for i := 0 to 99 do begin
x:=e*sin(Value);
Value := pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
end;

Recursion is often used where you don't know how many times one needs to carry
out a function because one is enumerating an unknown number of items.

Alan Lloyd
alangll...@aol.com

## Re:Help with a recursive function needed...

THANK YOU!
That finally made it. I can now plot my Latitude and Longitude with great
position an a scanned Mercator chart.

Thank you all for helping!

/Magnus

##### Quote
"AlanGLLoyd" <alangll...@aol.com> wrote in message

news:20030317171312.19274.00001087@mb-fd.aol.com...
##### Quote
> In article <CEWdnUdHIJhys-ujXTW...@giganews.com>, "Gabriel Gonzalez"
> <no-s...@no-spam.com> writes:

> >function recurse(var  t,e: extended; var count : integer): extended;
> >var x : extended;
> >begin
> >  count:=count+1;
> >  if (count<100)then
> >  begin
> >     x:=e*sin(recurse(t,e, count));
> >     recurse:= pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
> >  end else begin
> >    recurse := ???????;
> >  end;
> >end;

> Surely this is no different from ...

> var
>   Value, t, e, x : extended;
>   i : integer;
> begin
>   t := ???;
>   e := ???;
>   Value := ????; // starting value
>   for i := 0 to 99 do begin
>     x:=e*sin(Value);
>     Value := pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
>   end;

> Recursion is often used where you don't know how many times one needs to
carry
> out a function because one is enumerating an unknown number of items.

> Alan Lloyd
> alangll...@aol.com

## Re:Help with a recursive function needed...

"AlanGLLoyd" <alangll...@aol.com> schreef in bericht
news:20030317171312.19274.00001087@mb-fd.aol.com...

##### Quote
> In article <CEWdnUdHIJhys-ujXTW...@giganews.com>, "Gabriel Gonzalez"
> <no-s...@no-spam.com> writes:

> Surely this is no different from ...

> var
>   Value, t, e, x : extended;
>   i : integer;
> begin
>   t := ???;
>   e := ???;
>   Value := ????; // starting value
>   for i := 0 to 99 do begin
>     x:=e*sin(Value);
>     Value := pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
>   end;

> Recursion is often used where you don't know how many times one needs to
carry
> out a function because one is enumerating an unknown number of items.

> Alan Lloyd
> alangll...@aol.com

It IS different. In your code the function is called 100 times.
In the original code from Magnus the code is called 99! (factorial(99))
times.
That differs a bit <g>
Alfred.

## Re:Help with a recursive function needed...

Do you get any real improvement in accuracy with the 100 step calculation.
Surely the FPU works at 80 bit (Extended) accuracy and isn't that accurate to
about 1 in 2**63 == 1 in 10**19. There are only approx 1.5 x 10**9 inches in
the circumference of the earth.

Alan Lloyd
alangll...@aol.com

## Re:Help with a recursive function needed...

You try to find the root of a function.
I find that your algorithm is very efficient for abs(t)>1, when written as

function f(x,t,e : extended) : extended;
begin
result:= pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
end;

function root(t,e : extended) : extended; // result:=x
var count : integer; x : extended;
begin
result:=-e; count:=0;
repeat
inc(count); x:=result;
result:=e*sin(f(x,t,e));
until (abs(result-x1)<1e-9) OR (count>100);
end;

Typically, count will stay below 10 for t near 2 and below 5 for higher t.
However, when abs(t)<1, the efficiency is lost. And when abs(t)<0.5 or so,
there may not even be convergence. I haven't looked into this further. If
t<1 is relevant, then you may need to do the iteration the other way around:
express x as a function of f.
TdN

## Re:Help with a recursive function needed...

##### Quote
Magnus L wrote:
> Hi,
> Can someone tell me if there is a fundamental error in the following
> recursive function. When i call it i sometimes get the correct return, but
> often i get an "Invalid Floating point operation" exception. Can someone
> help me before i go totally nuts...

> function recurse(var  t,e: extended; var count : integer): extended;
> var x : extended;
> begin
>   count:=count+1;
>   if (count<100)then
>   begin
>      x:=e*sin(recurse(t,e, count));
>      recurse:= pi/2-2*arctan(t*power((1-x)/(1+x),e/2 ));
>   end;
> end;

When you are dealing with transcendental functions that are known to have
"impossible inputs," such as "arctan()" has, the most probable culprit
would be that this is what's happening.

Ahh...  but this is *not* what's happening!  :-)

Your function does not provide for any result if "count >= 100!"  You do not
assign any return-value to the function at all in an "else" clause.
Therefore you get memory-garbage.

----------------------------------
Fast automatic Paradox/Delphi table repair at a click of a mouse!
http://www.sundialservices.com/products/chimneysweep

Go to page: [1] [2]