0

I'm adding 4 simple numbers in 4 places. Yet the last one of the sequence, intead of adding up to zero, returns an exponential number:

There are 4 sections just like this, of which is is the last one:

# Data check rules #4
# add the values of remaining Col5 values, and see if it = 0
my $sum_col5 = 0;
for my $i (0 .. $#listOfValues){
    print $listOfValues[$i][5] . "\n";
    $sum_col5 += $listOfValues[$i][5];
}

The output for the values in the print statement looks like this just before they are added to sum from the array:

-1.52
0.80
0.05
0.67

If you add those numbers, they are 0. But when the $sum_col5 is printed to the screen at the end (or later in a report file), the number is:

1.11022302462516e-16%

I ran a converter on it and that translates to:

0.000000000000000111022302462516

The previous 3 sequences have 4 similar simple numbers and all add up fine.

What could be going wrong?

dbonneville
  • 2,079
  • 4
  • 19
  • 26
  • That's a near zero value and is probably the result of a minor rounding error as floating point numbers don't always represent your values 100% accurately. They're good 99.999999999938421341% of the time. – tadman Nov 19 '14 at 17:01
  • 2
    See: http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate – Morrison Chang Nov 19 '14 at 17:02
  • 52/100 is periodic in binary just like 1/3 is periodic in decimal. It's impossible to store that number accurately as a floating point number. Same goes for the 80/100, 5/100 and 67/100. – ikegami Nov 19 '14 at 17:31

0 Answers0