简体   繁体   English

C ++中的计算问题

[英]Calculation issue in C++

So in this program i am trying to print out the standard deviation of a set of numbers the user enters in. The formula to calculate standard deviation is correct (or as correct as it needs to be) so that is not the problem, but when i run the program everything goes well until the console prints out the results. 因此,在此程序中,我尝试打印出用户输入的一组数字的标准偏差。用于计算标准偏差的公式是正确的(或根据需要正确),所以这不是问题,而是当我运行程序,一切正常,直到控制台打印出结果。 It prints that totalStandardDeviation = nan 它打印出totalStandardDeviation = nan

what exactly does than mean? 到底是什么意思? is nan the same as nil? nan和nil一样吗? has it lost the value somehow and not been able to find it? 它是否以某种方式失去了价值并无法找到它? thanks for any help you may provide. 感谢您提供的任何帮助。

#include <iostream>
#include <cmath>
using namespace std;

double returnStandardDeviation(double x, int counter);
double total;

int userInput = 1;
int counter = 0;
double x;
double x1;
double x2;

double standardDeviation;
double totalStandardDeviation;

int main(int argc, const char * argv[])
{
    cout << "Please enter a list of numbers. When done enter the number 0 \n";
    cin >> userInput;

    while (userInput != 0)                         // As long as the user does not enter 0, program accepts more data
    {
        counter++;
        x = userInput;
        returnStandardDeviation( x, counter);       // send perameters to function

        cout << "Please enter next number \n";
        cin >> userInput;
    }

    cout << "The standard deviation of your "
    << counter
    << " numbers is : "
    << returnStandardDeviation(x, counter);
    return 0;
}


double returnStandardDeviation(double x, int counter)
{
    x1 += pow(x,2);
    x2 += x;
    totalStandardDeviation = 0;
    totalStandardDeviation += (sqrt(counter * x1 - pow(x2,2))) / (counter * (counter - 1));
    return totalStandardDeviation;
}

NaN代表“不是数字”。

NaN can eg be the result of: NaN可能是例如以下结果:

- Dividing by zero
- Taking the square root of a negative number 

In your function, both of these could happen. 在您的职能中,这两种情况都可能发生。 Division by zero eg when counter is <= 1; 除以零,例如,当counter <= 1时; and x1 and x2 are uninitialized ( += adds the value on the right to their current value - which was never set, and is therefore random gibberish), which can easily lead to your function trying to take the square root of some value < 0. x1x2未初始化( +=在其当前值右边添加值-该值从未设置,因此是乱码),这很容易导致您的函数尝试取某个值<0的平方根。

This expression 这个表达

counter * x1 - pow(x2,2)

can very easily yield a negative number. 可以很容易地产生一个负数 You then proceed to take its square root. 然后,您开始扎根。 This would result in a nan . 这将导致nan

Next, this one 接下来这个

counter * (counter - 1)

yields 0 when counter is 1 . counter1时产生0 Dividing by zero gives nan . 除以零得到nan

Your formula is wrong. 您的公式是错误的。 You are either dividing by zero or taking the square root of a negative number. 您要么被零除,要么取负数的平方根。

Check your formula! 检查您的公式!

Additional info: 附加信息:

NaN is "Not a number". NaN是“不是数字”。 It is an IEEE floating point value that signals an invalid results, like log(-1), or sqrt(-4). 这是IEEE浮点值,表示无效结果,例如log(-1)或sqrt(-4)。

Additionally, know that Positive Infinity and Negative Infinity are also floating point values. 此外,请知道正无穷大和负无穷大也是浮点值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM