1.计算标准偏差C时的#QNAN0输出 [英] 1.#QNAN0 output when calculating standard deviation C
本文介绍了1.计算标准偏差C时的#QNAN0输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个用于计算标准偏差的函数,结果始终为"1.#QNAN0",我尝试以其他方式设置其格式,但找不到解决方案.昨天它在另一台计算机上工作,我能做些什么吗?
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");
}
推荐答案
一个 NaN 只能您的代码中包含三个起源:
A NaN can only have three origins in your code:
-
mean
是 NaN . - 在
std/=(count);
中,如果count
为 0 . - 在
std=sqrt(std);
中,如果std
此时为负数(在您的情况下似乎不可能).
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).
您应该调试代码,并观察count
和mean
值(或打印/导出)以查找原因.
You should debug your code and watch count
and mean
values (or print/export it) to find the why.
这篇关于1.计算标准偏差C时的#QNAN0输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文