function keeps returning NaN [closed]

2019-09-01 15:49发布

问题:

Hi guys i know what NaN(let me say i know the acronym stands for Not a Number) is but i don't understand why C++ returns it - The following is the approximation of the mathematical constant e - When using the debugger the functions evaluate fine, it's when writing to the console that it returns NaN

Thanks for any feedback

double Factorial(int k)
{
    if(k == 0)
        return 1;

    int value = 1;
    for(int i = k; i > 0; i--)
        value *= k;
    return value;
}

double e(int p)
{
    double value = 0.0;

    for(int i = 0; i < p; i++)
    {
        value += 1/Factorial(i);
    }
}

回答1:

You don't return a value in your e function.



回答2:

You forgot to return value at the end of e. I don't know when c++ stopped warning about missing returns.



标签: c++ math nan