Why is the output different when adding same numbers?
public class Test {
public static void main(String a[]) {
double[] x = new double[]{3.9, 4.3, 3.6, 1.3, 2.6};
System.out.println(">>>>>>> " + sum(x));
}
public static double sum(double[] d) {
double sum = 0;
for (int i = 0; i < d.length; i++) {
sum += d[i];
}
return sum;
}
}
Output is : 15.7
and if I interchange values
double[] x = new double[] {2.6, 3.9, 4.3, 3.6, 1.3};
I am getting Output as : 15.700000000000001
How do I get the same Output ?