In my Java
file I constructed a method to work out variance in a list of numbers in a text file.
The function is shown below
public void varience(File commaSeperated) {
List<String> dataToSplit = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(commaSeperated));
String currentLine;
while ((currentLine = br.readLine()) != null) {
String[] tempArray = currentLine.split(",");
for(String s : tempArray) {
dataToSplit.add(s.replaceAll("\t", "").replaceAll("\n", "").trim());
}
}
br.close();
} catch(Exception e) {
e.printStackTrace();
}
BigDecimal finalD = new BigDecimal("0");
int count = 0;
for(String temp : dataToSplit) {
finalD = finalD.add(new BigDecimal(temp));
count++;
}
System.out.println("Elements: " + count + ", Sum: " + finalD + ", Average: " + (finalD.divide(new BigDecimal(count))));
BigDecimal average = finalD.divide(new BigDecimal(count));
count = 0;
BigDecimal finalVar = new BigDecimal(0);
for(String temp : dataToSplit) {
BigDecimal tempBD = ((new BigDecimal(temp)).subtract(average)).pow(2);
finalVar = finalVar.add(tempBD);
count++;
System.out.println("Original Value: " + temp + ", Pass: " + count + ", Adding: " + tempBD + ", CurrentValue: " + finalVar);
}
System.out.println(finalVar.divide(new BigDecimal(count))); //This prints the variance
}
The list within the file is as follows
7.1,7.1,7.1,7.1,7.2,7.2,7.2,7.2,7.2,7.2,7.2,7.3,7.3,7.3,7.3,7.3,7.3,7.3,7.3,7.3,7.3,7.3,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.4,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.5,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.6,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.7,7.8,7.8,7.8,7.8,7.8,7.8,7.9,7.9
Using this function the program prints the value of the variance to be 0.034899
. However, when I decided to pass the list of numbers through an external source a different value, 0.034898999999923
, was returned. This was worked out here.
So the question: Is my code wrong in some way, or truncating the precision of the final value for some reason, or is my code correct and the answer returned by the external source incorrect?