运行时异常,递归太深 [英] Runtime exception, recursion too deep

查看:132
本文介绍了运行时异常,递归太深的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换的伪代码这里成C#,并将其递归重复10000次。但我得到一个C#运行时错误,计算器异常 9217 倍。 ?我怎样才能防止这种



修改如果它可以帮助任何人,这里的代码:

 私人双CalculatePi(INT MAXRECURSION)
{
返回2 * CalculatePi(MAXRECURSION,1);
}

私人双CalculatePi(INT MAXRECURSION,int i)以
{
如果(I> = MAXRECURSION)
返回1;
返回1 + I /(2.0 * I + 1)* CalculatePi(MAXRECURSION,I + 1);
}

双PI = CalculatePi(10000); //万递归



EDIT2 所以,大家似乎都认为我需要这个转换迭代......有谁能够提供一些代码吗?我似乎无法编写适用于任何迭代代码...



修改感谢保罗·里克对这个答案,我测试过,和它的作品:

 私有静态双CalculatePi(INT MAXRECURSION)
{
双重结果= 1;
的for(int i = MAXRECURSION; I> = 1;我 - )
{
结果= 1 + I /(2.0 * I + 1)*结果;
}
返回结果* 2;
}


解决方案

在C#编译器,将编译该罚款。在运行时,但是,您可能结束了一个计算器异常(我的机器上10000工作正常,但后来死了)。



这是不是一个无限递归例外 - 堆栈溢出是正是它说;调用一个方法是指将堆栈上的信息,并删除它调用返回时。您有10,000个电话,都没有回来,所以堆栈已满,并且抛出异常。



您可以很容易在这种特殊情况下,通过去除递归解决这个问题,并反复运行。在一般情况下,有办法通过迭代,尾递归优化,并延续传球删除递归



下面是一个简单的直接翻译为迭​​代式的:

 私有静态双CalculatePi(INT MAXRECURSION)
{
双重结果= 1;
表示(INT I = MAXRECURSION -1; I> = 1;我 - )
{
结果= 1 + I /(2.0 * I + 1)*结果;
}
返回结果* 2;
}


I converted the pseudo-code here into C#, and have it recursively repeat 10,000 times. But I get a C# runtime error, StackOverflow Exception after 9217 times. How can I prevent this?

EDIT If it helps anybody, here's the code:

    private double CalculatePi(int maxRecursion)
    {
        return 2 * CalculatePi(maxRecursion, 1);
    }

    private double CalculatePi(int maxRecursion, int i)
    {
        if (i >= maxRecursion)
            return 1;
        return 1 + i / (2.0 * i + 1) * CalculatePi(maxRecursion, i + 1);
    }

    double pi = CalculatePi(10000); // 10,000 recursions

EDIT2 So everybody seems to agree that i need to convert this to iterative... can anybody give some code? I can't seem to write any iterative code that works...

EDIT Thanks to Paul Rieck for this answer, which I tested, and it works:

    private static double CalculatePi(int maxRecursion)
    {
        double result = 1;
        for (int i = maxRecursion; i >= 1; i-- )
        {
            result = 1 + i / (2.0 * i + 1) * result;
        }
        return result * 2;
    }

解决方案

The "c# compiler" will compile this fine. At runtime, however, you may end up with a StackOverflow exception (on my machine, 10,000 works fine, but dies later).

This is not an "infinite recursion exception" - stack overflow is exactly what it says; calling a method means putting information on the stack and removing it when the call returns. You have 10,000 calls and none have returned, so the stack is full and the exception is thrown.

You can fix this fairly easily in this particular case by removing the recursion and running iteratively. In the general case, there are ways to remove recursion via iteration, tail-recursion optimization, and continuation passing.

Here's a quick direct translation to an iterative style:

private static double CalculatePi(int maxRecursion)
        {
            double result = 1;
            for (int i = maxRecursion -1; i >= 1; i-- )
            {
                result = 1 + i / (2.0 * i + 1) * result;
            }
            return result * 2;
        }

这篇关于运行时异常,递归太深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆