473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

(academic) math recursion question

consider:
Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
A = A/2;
try
{
A = recurse(A);
}
catch
{
return steps;
}
return 1; //To satisfy return on all code paths (never reached).
}

At the point of underflow I would expect a stack overflow exception to
trigger the directive in the catch block. But it does not. Rather, it halts
execution in the try block with: "An unhandled exception of type
'System.StackOverflowException' occurred".

Stipulating that a recursive procedure be used, and stipulating that testing
to the known underflow threshold not be performed, how can this
implementation be made functional?



--
mark b
Jun 27 '08 #1
8 1398
mark submitted this idea :
consider:
Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
A = A/2;
try
{
A = recurse(A);
}
catch
{
return steps;
}
return 1; //To satisfy return on all code paths (never reached).
}

At the point of underflow I would expect a stack overflow exception to
trigger the directive in the catch block. But it does not. Rather, it halts
execution in the try block with: "An unhandled exception of type
'System.StackOverflowException' occurred".

Stipulating that a recursive procedure be used, and stipulating that testing
to the known underflow threshold not be performed, how can this
implementation be made functional?
From MSDN:
In prior versions of the .NET Framework, your application could catch a
StackOverflowException object (for example, to recover from unbounded
recursion). However, that practice is currently discouraged because
significant additional code is required to reliably catch a stack
overflow exception and continue program execution.

Starting with the .NET Framework version 2.0, a StackOverflowException
object cannot be caught by a try-catch block and the corresponding
process is terminated by default. Consequently, users are advised to
write their code to detect and prevent a stack overflow. For example,
if your application depends on recursion, use a counter or a state
condition to terminate the recursive loop. Note that an application
that hosts the common language runtime (CLR) can specify that the CLR
unload the application domain where the stack overflow exception occurs
and let the corresponding process continue. For more information, see
ICLRPolicyManager Interface and Hosting the Common Language Runtime.
http://msdn.microsoft.com/en-us/libr...exception.aspx

Hans Kesting
Jun 27 '08 #2
http://msdn.microsoft.com/en-us/libr...exception.aspx

"Starting with the .NET Framework version 2.0, a
StackOverflowException object cannot be caught by a try-catch block
and the corresponding process is terminated by default."
"A thrown StackOverflowException cannot be caught by a try-catch
block. Consequently, the exception causes the process to terminate
immediately. "

Now, truth be told I've never looked very hard [if I blow the stack, I
assume I've done something very, very wrong] - but AFAIK there is no
obvious way to do it... again: I've not looked very hard!

Marc
Jun 27 '08 #3
mark wrote:
consider:
Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
A = A/2;
try
{
A = recurse(A);
}
catch
{
return steps;
}
return 1; //To satisfy return on all code paths (never
reached). }

At the point of underflow I would expect a stack overflow exception to
trigger the directive in the catch block. But it does not. Rather, it
If you want you detect underflow, best put "A = A/2;" in a checked block.
Or test if ((A/2)*2 == A).

But in any case I don't think you understand exception handling, based on
your comment on the return statement. Assuming that a catchable exception
is thrown, the next outer stack frame will catch it and execute "return
steps;" thereby returning normally. Thus the next outer frame will finish
the try block without exception, and hit the "never reached" return 1
statement, also returning normally. It is trivial to show by induction (you
did say math question) that any deeper number of calls will also ultimately
return 1. The key point is that the exception is caught by the innermost
enclosing (matching) exception handler, is does not jump to the outermost
frame of the recursion.

Perhaps you meant the catch block to say "return recurse(A);"? In that case
the "return 1;" outside the block would indeed be unreachable and could be
removed.
halts execution in the try block with: "An unhandled exception of type
'System.StackOverflowException' occurred".

Stipulating that a recursive procedure be used, and stipulating that
testing to the known underflow threshold not be performed, how can
this implementation be made functional?

Jun 27 '08 #4
OK, Something like this works:

Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
if ((A / 2) * 2 != A)
{
return steps-1;
}
A = A/2;
return recurse(A);
}

It's interesting that the A/2 in the conditional does not throw the overflow
error.
--
mark b
"Ben Voigt [C++ MVP]" wrote:
mark wrote:
consider:
Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
A = A/2;
try
{
A = recurse(A);
}
catch
{
return steps;
}
return 1; //To satisfy return on all code paths (never
reached). }

At the point of underflow I would expect a stack overflow exception to
trigger the directive in the catch block. But it does not. Rather, it

If you want you detect underflow, best put "A = A/2;" in a checked block.
Or test if ((A/2)*2 == A).

But in any case I don't think you understand exception handling, based on
your comment on the return statement. Assuming that a catchable exception
is thrown, the next outer stack frame will catch it and execute "return
steps;" thereby returning normally. Thus the next outer frame will finish
the try block without exception, and hit the "never reached" return 1
statement, also returning normally. It is trivial to show by induction (you
did say math question) that any deeper number of calls will also ultimately
return 1. The key point is that the exception is caught by the innermost
enclosing (matching) exception handler, is does not jump to the outermost
frame of the recursion.

Perhaps you meant the catch block to say "return recurse(A);"? In that case
the "return 1;" outside the block would indeed be unreachable and could be
removed.
halts execution in the try block with: "An unhandled exception of type
'System.StackOverflowException' occurred".

Stipulating that a recursive procedure be used, and stipulating that
testing to the known underflow threshold not be performed, how can
this implementation be made functional?


Jun 27 '08 #5
mark wrote:
OK, Something like this works:

Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
if ((A / 2) * 2 != A)
{
return steps-1;
}
A = A/2;
return recurse(A);
}

It's interesting that the A/2 in the conditional does not throw the
overflow error.
Why would it? It can only underflow, not overflow.

You do realize that the StackOverflowException was due to infinite recursion
and not due to floating point underflow?

Here is an improvement though:

Int64 steps = 0;
public Int64 recurse(double A)
{
double A2 = A / 2;
if (A2 * 2 != A)
{
return steps;
}
steps++;
return recurse(A2);
}

Or, going purely functional:

public Int64 recurse(double A)
{
double A2 = A / 2;
if (A2 * 2 != A)
{
return 0;
}
return recurse(A2) + 1;
}
Jun 27 '08 #6
You do realize that the StackOverflowException was due to infinite recursion
and not due to floating point underflow?

My problem was that I did not understand this.

Thanks
--
mark b
Jun 27 '08 #7
mark presented the following explanation :
OK, Something like this works:

Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
if ((A / 2) * 2 != A)
{
return steps-1;
}
A = A/2;
return recurse(A);
}

It's interesting that the A/2 in the conditional does not throw the overflow
error.
When I try this (in SnippetCompiler) I still get a stack overflow. I
added some Writeline statements to find out it reached about 40.000
steps. The value of A was 0 from step 1075 (I started with 1.0).

The test "(A / 2) * 2 != A" is the problem here: it is true for A==0,
but the bigger problem is: will it work correctly for a non-zero value?

Hans Kesting
Jun 27 '08 #8
Hans,

With VS2005 C# code, the recursion is stable for values of A except A==0
(for which A/0 does not change and is never equal to A).

--
mark b
"Hans Kesting" wrote:
mark presented the following explanation :
OK, Something like this works:

Int64 steps = 0;
public Int64 recurse(double A)
{
steps += 1;
if ((A / 2) * 2 != A)
{
return steps-1;
}
A = A/2;
return recurse(A);
}

It's interesting that the A/2 in the conditional does not throw the overflow
error.

When I try this (in SnippetCompiler) I still get a stack overflow. I
added some Writeline statements to find out it reached about 40.000
steps. The value of A was 0 from step 1075 (I started with 1.0).

The test "(A / 2) * 2 != A" is the problem here: it is true for A==0,
but the bigger problem is: will it work correctly for a non-zero value?

Hans Kesting
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
13
by: Comet | last post by:
Hi I have had a look on the net with regards to whether you can sell applications you created with Visual Studio .NET 2003 Academic. It seems to be as clear as mud! Can anyone help? Are you?...
24
by: Mary Rosemount | last post by:
http://www.studentsforacademicfreedom.org/index.html
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
6
by: Brett | last post by:
Is there a restriction with the academic version of VS.NET 2003 in the way of distributing an EXE? Or, what is the difference in this version and the commercial versions with respect to...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
12
by: Muzammil | last post by:
i want good practice over recursion. can any one give me links for recursion questions site.?? or question.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.