0

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?

Dan
  • 7,286
  • 6
  • 49
  • 114
  • "the answer returned by the external source incorrect?" The answer from the external source is using floating point math, and thus is imprecise. – Andy Turner Jul 17 '16 at 19:19
  • @AndyTurner Okay. Thank you for that information. May I ask how you knew `The answer from the external source is using floating point math, and thus is imprecise`? – Dan Jul 17 '16 at 19:22
  • You've not got enough decimal places in your data to give that many dp in the result. – Andy Turner Jul 17 '16 at 19:24
  • @AndyTurner Okay. Thank you – Dan Jul 17 '16 at 19:25
  • 1
    BTW, particularly as you are using `BigDecimal`, you should probably try to avoid doing two iterations through the data: you can do it in a single for loop (which avoids unnecessary object creation). – Andy Turner Jul 17 '16 at 19:26

0 Answers0