1.#QNAN0 output when calculating standard deviation C

跟風遠走 提交于 2019-12-12 04:48:43

问题


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); if count is 0.
  • In std=sqrt(std); if std 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

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