I have such a program:
public class Main { public static void main(String[] args) { double nominal = 1.0; double sum = 0; for (int i = 0; i < 10; i++) { sum += 0.1; System.out.println("sum = " + sum); } if (nominal == sum) { System.out.println("Числа равны!"); } else { System.out.println("Числа не равны!"); } } }
According to the meaning, the result of the addition of a number in the cycle should be 1, but the result is this ..:
sum = 0.1 sum = 0.2 sum = 0.30000000000000004 sum = 0.4 sum = 0.5 sum = 0.6 sum = 0.7 sum = 0.7999999999999999 sum = 0.8999999999999999 sum = 0.9999999999999999 Числа не равны!
Why is a double
not added as it should?
BigDecimal
. - Arsenicum