function keeps returning NaN [closed]

℡╲_俬逩灬. 提交于 2019-12-25 03:59:23

问题


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.



来源:https://stackoverflow.com/questions/8189256/function-keeps-returning-nan

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!