问题
I've written a function for calculating the standard deviation and the result is always '1.#QNAN0', I've tried formatting it in different ways but I can't find a solution. It was working on a different computer yesterday, is there anything I can do?
void CalcStandardDeviation(int count, int* nums, double mean, FILE* output){
int k;
double std=0,a;
for (k=0; k<count; k++){
a=nums[k]-mean;
std=std+(a*a);
}
std/=(count);
std=sqrt(std);
fprintf(output,"Standard deviation: %f\r\n",std);
fprintf(output,"\r\n");
}
回答1:
A NaN can only have three origins in your code:
mean
is a NaN.- In
std/=(count);
ifcount
is 0. - In
std=sqrt(std);
ifstd
at this point is a negative number (seems impossible in your case).
You should debug your code and watch count
and mean
values (or print/export it) to find the why.
来源:https://stackoverflow.com/questions/29210975/1-qnan0-output-when-calculating-standard-deviation-c