我正在嘗試運行一個集成程序,但是我一直在計算時纔得到nan。我不知道我的代碼有什麼問題。C++ nan不斷提出整合
#include <iostream>
#include <cmath>
using namespace std;
int main(){
cout << "For integration up \n";
for (int z=0; z<=5; z++){
int i=1;
float nathan [6] = {pow(10,2), pow(10,3), pow(10,4), pow(10,5),pow(10,6), pow(10,7)};
int h= nathan[z];
int n=0;
double x= (h-i)/h;
double y= (h-i)/h;
double t= 0;
while(n <= h){
if(n == 0){
t += (x/3)*(1/y);
}else if(n==h){
t+= (x/3)*(1/y);
}else if(n%2 ==1){
t+= (4*x/3)*(1/y);
}else{t+= (2*x/3)*(1/y);
}
y= x+y;
n = n+1;
}
cout << "The integration of 1/x for N = "<< nathan[z] <<" is equal to " << t << endl;
}
}
能有人幫我這...
使用調試器。 – chris 2015-04-05 18:13:50