C# 中递归方法中的奇怪值

Strange values in a recursion method in C#

作为练习,我们的一名学徒应该实现一个递归正弦函数。 (使用广义连分数) 我试着帮助他,相比之下,我已经完成了相当多的编码工作,但现在我遇到了一个我不明白的问题。

我有一个有效的功能。 我不明白的是为什么我的前三四次尝试都失败了。 我试着一步一步地调试这个东西,但我无法查明我的错误。我真的很想知道我错过了什么。

当心,因为代码并不像它应该的那样漂亮。这是我在 5 分钟内(多次)写的一个快速而肮脏的概念证明。

这是不起作用的代码:

// number = the angle in radian

static double sinus(double number, double exp = 1, bool mustAdd = false, double precision = 0.000001)
{
    if (number < 0) throw new ArgumentException("sinus");
    if (number == 0) return 0;
    double result = ((Math.Pow(number, exp)) / factorial(exp));
    Console.WriteLine(result);

    if (result > precision)
    {
        if (mustAdd)
            return result += sinus(number, exp + 2, !mustAdd);
        else
            return result -= sinus(number, exp + 2, !mustAdd);
    }
    else
        return result;
}

我正在使用中间值打印每个迭代,以验证一切是否都在相应地工作。值正确。

这是我想出的工作代码(是的,它也很脏):

static double Altersinus(double number, double exp = 1, bool mustAdd = true, double precision = 0.000001, double result = 0)
{
    if (number < 0) throw new ArgumentException("altersinus");
    if (number == 0) return 0;

    double tmp = ((Math.Pow(number, exp)) / factorial(exp));
    Console.WriteLine(tmp);

    if (tmp > precision)
    {
        if (mustAdd)
            result += tmp;
        else
            result -= tmp;
        result = Altersinus(number, exp + 2, !mustAdd, precision, result);

    }

    return result;
}

我也在写中间值,和没用的函数一模一样

再说一次,我真的不是在寻找解决方案,也不着急。我只是想了解为什么它不起作用。我想知道我的两种方法在技术上有什么不同。

任何想法将不胜感激。

干杯。

编辑

我尝试了两个函数,值为 3.14159265358979(大约 180 度)

两个函数都在打印这些中间值:

3.14159265358979
5.16771278004997
2.55016403987735
0.599264529320792
0.0821458866111282
0.00737043094571435
0.000466302805767612
2.19153534478302E-05
7.95205400147551E-07

无效的方法returns结果-3.90268777359824,完全错误

有效的 returns -7.72785889430639E-07。大致对应于零。

我想通了。

让我们用 'nx' 代替微积分,其中 x 是指数,n 是数字。

在有效的函数中,我实际上是这样的:

Sine(n)=n1/1! - n3/3! + n5/5! - nx/x!...

但不起作用的那个略有不同。它在做其他事情:

Sine(n)=n1/1! - (n3/3! + (n5/5! - (nx/x!...)))

这里的关键是括号。 由于减法,它对微积分的影响很大。

如果只有加法就不会造成任何问题。