1

Here's the textbook problem I need to solve:

Write a recursive function that solves the following for a given number n: enter image description here

The result I get for n = 6 is 120.184056

The result I want for n = 6 is 120.184052

The result I get for n = 7 is 720.147296

The result I want for n = 7 is 720.147339

My question is, why are my decimals off? The error seems to be increasing as I increase the n.

My code:

#include <stdio.h>
#include <stdlib.h>

double factorial(int num)
{
    if(num==1)
    {
        return 1;
    }
    else
    {
        return num * factorial(num-1);
    }
}

double recurisve(int n, int c)
{
    double addend, enumerator;

    if(c%2==1)
    {
        addend = c/2+1;
        enumerator = factorial(n - c/2);
    }
    else if(c%2==0)
    {
        addend = n - (c-1)/2;
        enumerator = factorial((c-1)/2+1);
    }

    if(c==n)
    {
        return addend + enumerator;
    }
    else
    {
        return addend + enumerator / recurisve(n, c+1);
    }
}

int main()
{
    int n;

    scanf("%d",&n);

    printf("%lf",recurisve(n,1));

    return 0;
}
David Ilic
  • 165
  • 1
  • 7
  • 1
    You picked the wrong type. You should probably have used an `int` or `long`. A floating point number `float` or `double` loses precision with big values. – Cheatah Dec 14 '20 at 11:56
  • I'm not sure I understand you. How can an `int` or `long` have decimal points? – David Ilic Dec 14 '20 at 11:58
  • 1
    [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – J... Dec 14 '20 at 11:59
  • Hmm, so is there no way to fix it? Rounding to 6 decimals, like someone in that question suggested, doesn't seem to work – David Ilic Dec 14 '20 at 12:02
  • 1
    The factorial function should not return `double`, factorials are always integer values. So you already lose precision there where you don't need to. The division on the other hand cannot be fixed. – Cheatah Dec 14 '20 at 12:04
  • You're right, but I did read the top answer and was hoping for a quick fix – David Ilic Dec 14 '20 at 12:04
  • @Cheatah Yep, my factorial returned int at first, but it was one of the "fixes" I tried. Thanks – David Ilic Dec 14 '20 at 12:05
  • 2
    @DavidIlic Precision problems don't always have quick fixes. You need to determine where in your algorithm you are running into the limitation of your datatypes and then you need to either change your algorithm or change your datatypes to preserve the precision you need. It requires some analysis of the specific problem. – J... Dec 14 '20 at 12:09
  • @J... Thank you, that's what I'll be doing after I read through that question. – David Ilic Dec 14 '20 at 12:13
  • 1
    To answer your tangential question (how can integers have decimals), see [Decimal Data Type](https://en.wikipedia.org/wiki/Decimal_data_type) and [Fixed point arithmetic](https://en.wikipedia.org/wiki/Fixed-point_arithmetic) – J... Dec 14 '20 at 12:14
  • In the old days, you had to do this with calculators as well. Often things like physics problems combine very large and very small numbers in complex equations. Punching them naively into a calculator would often lead you into nonsense because of precision breakdown. You had to think about the scale of all the terms in the equation and come up with a sane order to punch them into the calculator so that you eased into the answer you were looking for, staying safely within the bounds of the machine's precision. – J... Dec 15 '20 at 02:13

0 Answers0