从文件差异计算读取 [英] Read from File Variance Calculation

查看:117
本文介绍了从文件差异计算读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Jerry Coffin

@Jerry Coffin

我得到逻辑,
while(File >> value)// while input from file is true
...做计算。
当我实现这个计数器只去了1&它的价值是非常高的。有时是错的,但我不知道什么。档案有效

I get the logic, while(File>>value)//while input just taken from file is true .... do computation. Yet when I implemented this the counter only went to 1 & it's value was very high. Sometime is wrong, but I have no idea what. The file is valid

File.open(FileName, ifstream::in);  
while(File>>value){  
    ++counter;  
    sum += value;  
    sumsqr+= value * value;  
}  
average=sum/counter;  
variance = sumsqr/counter - average*average;  
File.close();  

下面是我使用的text.txt
$ 23244564的输入文件的内容b $ b 1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
14864152412505862058641048186386848408

here's the contents of the input file I am using "text.txt" 23244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 14864152412505862058641048186386848408

推荐答案

不幸的是,(至少)三个答案都引述你的而(!File.eof()) 没有评论的事实,这只是纯粹的错误。你想要的是这样的:

Sadly, (at least) three answers have quoted your while (!File.eof()) without commenting on the fact that this is just plain wrong. What you want is something like this:

while (File>>value) {
    ++counter;
    sum += value;
    sumsqr += value * value;
}
average = sum/counter;
variance = sumsqr/counter - average * average;

使用 while(!File.eof code>是阴险的 - 你通常会得到看起来合理,并且实际上公平接近正确的结果。问题是 eof()直到尝试读取文件后才会变为true,并且尝试读取失败。当它失败时, value 将仍然具有您读取的最后一个值,因此它将像列表中的最后一个数字是真的有两次(例如,如果您的文件包含21数字,你的循环将执行22次,并在22 nd 迭代,它将再次使用21 st 编号。这将抛弃你的计算,但通常不够,它是立即显而易见的 - 几乎最糟糕的可能类型的错误。

The bug from using while (!File.eof()) is insidious -- you'll typically get results that look reasonable, and are actually fairly close to correct. The problem is that eof() doesn't become true until after you've attempted to read from the file, and the attempted read has failed. When it fails, value will still have the last value you read, so it'll act like the last number in the list was really there twice (e.g., if your file contained 21 numbers, your loop would execute 22 times, and on the 22nd iteration, it would use the 21st number again). This will throw your calculations off a bit, but usually not enough that it's immediately obvious -- nearly the worst possible kind of bug.

编辑:这里是一个完整的测试程序:

Here's a complete test program:

#include <fstream>
#include <iostream>

double variance(std::istream &File) {
    double value, average, sum, counter, sumsqr, variance;
    while (File>>value) {
        ++counter;
        sum += value;
        sumsqr += value * value;
    }
    average = sum/counter;
    variance = sumsqr/counter - average * average;
    return variance;
}

double variance2(std::istream &File) {
    double value, average, sum, counter, sumsqr, variance;
    while (!File.eof()) {
        ++counter;
        File >> value;
        sum += value;
        sumsqr += value * value;
    }
    average = sum/counter;
    variance = sumsqr/counter - average * average;
    return variance;
}

int main() { 
    std::ifstream in("data.txt");
    double v1 = variance1(in);
    in.clear();
    in.seekg(0);
    double v2 = variance2(in);

    std::cout << "Using \"while (file>>value)\"" << v1 << "\n";
    std::cout << "Using \"while (!file.eof())\"" << v2 << "\n";
    return 0;
}

这里有一些测试数据:

1
2
3
4
5
6
7
8
9
10

得到:

Using "while (file>>value)": 8.25 
Using "while (!file.eof())": 9.17355

作为一个交叉检查,我做了Excel中的计算,使用两组数据:

As a cross-check, I did the computation in Excel, using two sets of data:

1           1
2           2
3           3
4           4
5           5
6           6
7           7
8           8
9           9
10          10
8.25        10
            9.173553719

每列中的最后一行是对前面的数据执行VARP的公式的结果。请注意,我的函数与Excel为正确的输入数据生成的函数匹配。使用 while(!file.eof())的函数与Excel生成的函数与最后一个重复的数字匹配。

The last line in each column is the result of a formula doing "VARP" on the preceding data. Note that my function matches with what Excel produces for the correct input data. The function using while (!file.eof()) matches with what Excel produces with the last number duplicated.

我甚至不能开始猜测发生了什么,使循环只运行一次,并读取一个不正确的值。没有能猜到或重现的问题,恐怕我不能提供太多的有用的建议,如何解决它的方式。

I can't even begin to guess what's happening to make the loop run only once and read an incorrect value. Without being able to either guess at or reproduce the problem, I'm afraid I can't provide much in the way of useful suggestions about how to fix it.

这篇关于从文件差异计算读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆