我应该怎么做才能使 "nan" 不显示在控制台中?

What should I do so that "nan" doesn't show up in console?

我的老师布置了这个作业。基本上我有两个数字,a 和 b。我必须向控制台 answer of this formula 显示每个添加的 'a' 数字 h=(b-a)/10 但在控制台中我只看到 nan。我该如何解决这个错误? 我的代码是:

#include <iostream>
#include <math.h>
using namespace std;
double s(double x){
    long f = 1;
    long double anw=1;
    for(int k=1;k<=100;k++){
        f=k*f;
         anw=((pow((x-1),k)*pow(log(3),k))/f)+anw;
    }
    return anw;
}
int main(){
    double a=0.2, b=0.8, h;
    h=(b-a)/10;
    for(double x=a; x<=b ;x+=h){
        cout<<"s(x)="<<s(x)<<endl;
    }
    return 0;
}

抱歉我的英语不好!

您在 f=k*f; 中遇到有符号整数溢出,所以我建议您将 f 设为 double:

double s(double x){
    double f = 1;              // double
    long double anw=1;
    for(int k=1;k<=100;k++){
        f = k * f;             // or else you get a signed integer overflow here
        // f *= k;             // a simpler way of writing the above
        anw=((pow((x-1),k)*pow(log(3),k))/f)+anw;
    }
    return anw;
}

另一个注意事项:包括 C++ 头文件 cmath 而不是 C 头文件 math.h