This week's book giveaway is in the Design forum. We're giving away four copies of Building Microservices and have Sam Newman on-line! See this thread for details.

Hi, I'm just learning Java and have run into a problem I can't figure out. I haven�t found it in any of the books I am reading. This problem was posted on the Java Ranch Cattle Drive. The problem was to total the amount of 0.1 1000 times and compare the total to 100. Pretty simple problem, or so I thought. I coded the following and then got the result at the end. I reduced the total number of summing to 10 for display purposes only. import javax.swing.*; public class Sum { public static void main( String[] args ) { double sum = 0; double i = 0.1; for (int x = 0 ; x < 10 ; x ++)<br /> { <br /> sum += i; <br /> System.out.println("The total " + sum + " " + i);<br /> }<br /> if (sum == 100) <br /> {<br /> System.out.println("The total " + sum + " is equal to 100 " + i); <br /> }<br /> else <br /> {<br /> System.out.println("The total " + sum + " does not equal 100 " + i); <br /> }<br /> }<br /> }<br /> Produces this result. <br /> C:\Java>java Sum The total 0.1 0.1 The total 0.2 0.1 The total 0.30000000000000004 0.1 The total 0.4 0.1 The total 0.5 0.1 The total 0.6 0.1 The total 0.7 0.1 The total 0.7999999999999999 0.1 The total 0.8999999999999999 0.1 The total 0.9999999999999999 0.1 The total 0.9999999999999999 does not equal 100 0.1 What is going on here? Why does this occur? The first number is the value of sum and the second number is the value of �i�. I�ve tried it with float numbers and got the following results. C:\Java>java Sum The total 0.1 0.1 The total 0.2 0.1 The total 0.3 0.1 The total 0.4 0.1 The total 0.5 0.1 The total 0.6 0.1 The total 0.70000005 0.1 The total 0.8000001 0.1 The total 0.9000001 0.1 The total 1.0000001 0.1 The total 1.0000001 does not equal 100 0.1 Any help would be greatly appreciated. Thanks David

This is a strange problem. I don't see anything wrong with your logic, as far as I can tell, it should work with either double or floats, but when I complied and ran the code you provided, I got the exact same results. I'm not sure why you are importing swing, as it's not used in this probgram, but omitting his line doesn't have any effect on the output.

There is nothing wrong with your code. The reason for the output is the way your computer handles math -- in particular -- double and float. When I did a similar program on my computer, I got the same result. You can fix your output to however many decimal places you want by rounding or formatting. Hope this helps.

David StJohn
Greenhorn

Joined: Dec 22, 2000
Posts: 3

posted

0

I'm still confused. I changed the code to add ten individual float integers and I still got erronous values. How would you add float integers together and expect to get a valid value. Here is the new code and the result. public class Sum2 { public static void main( String[] args ) { float sum = 0F; float i = 1.1F; float j = 1.1F; float k = 1.1F; float l = 1.1F; float m = 1.1F; float n = 1.1F; float o = 1.1F; float p = 1.1F; float q = 1.1F; float r = 1.1F;

sum += i; System.out.println("The total " + sum + " " + i); sum += j; System.out.println("The total " + sum + " " + i); sum += k; System.out.println("The total " + sum + " " + i); sum += l; System.out.println("The total " + sum + " " + i); sum += m; System.out.println("The total " + sum + " " + i); sum += n; System.out.println("The total " + sum + " " + i); sum += o; System.out.println("The total " + sum + " " + i); sum += p; System.out.println("The total " + sum + " " + i); sum += q; System.out.println("The total " + sum + " " + i); sum += r; System.out.println("The total " + sum + " " + i);

if (sum == 100) { System.out.println("The total " + sum + " is equal to 100 " + i); } else { System.out.println("The total " + sum + " does not equal 100 " + i); } } } And the result was C:\Java>java Sum2 The total 1.1 1.1 The total 2.2 1.1 The total 3.3000002 1.1 The total 4.4 1.1 The total 5.5 1.1 The total 6.6 1.1 The total 7.7 1.1 The total 8.8 1.1 The total 9.900001 1.1 The total 11.000001 1.1 The total 11.000001 does not equal 100 1.1 It still doesn't make sense. I understand about formating the output. I just don't understand why this works like this on what I consider a simple calculation.

Here the reason for your problem: 0.1 cannot be represented as double... (!). Ok this post is old, but I recently found this stuff while reading the BigDecimal javadoc: BigDecimal(double). The recommended way is to use the BigDecimal(String) constructor.