sum "product of double type" calculation not percise
public class Test3 {
public static void main(String[] args) {
double weight[][] = {
{ 0.0, 1.0, -3.0, 2.0, -0.3, -2.1, -1.0},
{ 1.0, 0.0, 1.0, 1.0, 3.0, -4.0, 0.0},
{-3.0, 1.0, 0.0, 0.0, 1.0, -1.0, -1.0},
{ 2.0, 1.0, 0.0, 0.0, 0.0, -2.4, 3.3},
{-0.3, 3.0, 1.0, 0.0, 0.0, 1.0, 1.0},
{-2.1, -4.0, -1.0, -2.4, 1.0, 0.0, -1.0},
{ 1.0, 0.0, -1.0, 3.3, 1.0, -1.0, 0.0}
};
double input[] = {1.000000 ,-1.000000 ,1.000000 ,-1.000000 ,1.000000 ,-1.000000 ,1.000000};
double sum = 0.0;
for (int i = 0; i < 7; i++ ){
sum = 0.0;
for (int j = 0; j < 7; j++ ){
sum += weight[j] * input[j];
}
System.out.println("sum " + i + " = " + sum);
}
}
}
I get the result as below
sum 0 = -5.199999999999999
sum 1 = 8.0
sum 2 = -3.0
sum 3 = 6.699999999999999
sum 4 = -2.3
sum 5 = 3.3
sum 6 = -1.2999999999999998
how come?
It suppose
sum 0 = -5.2
sum 3 = 6.7
sum 6 = -1.3

