# Thread: Two ways of summing a total, two different results!

1. Member Join Date
Apr 2013
Posts
4
Rep Power
0

## Two ways of summing a total, two different results!

I'm using two different methods to find a total, as a check, since the value should both be the same. Simplifying the code as much as possible:

Java Code:
```{ // loop that runs a few million times
if (win >= win2)  {payPerCombo[i] = payPerCombo[i]+win;  winnings+=win;}
else              {payPerCombo[j] = payPerCombo[j]+win2; winnings+=win2;}
}

System.out.println(winnings +"\n");

winnings=0;
for (i=0; i<=33; i++) winnings+= payPerCombo[i];
System.out.println(winnings +"\n");```
The output is:

2.145422E7

2.1293528E7

They ought to be the same, since I'm incrementing each side by the same amount.

I'm not using the variables (payPerCombo[] and winnings) anywhere else in the code. They're both int, so they should have plenty of capacity for the numbers I'm using.

FWIW, the second result is the one that's correct. No idea why the first one is inflated.

What the heck?  Reply With Quote

2. ## Re: Two ways of summing a total, two different results!

If all your numbers are ints, how come you have floating point type results?

kind regards,

Jos  Reply With Quote

3. Member Join Date
Apr 2013
Posts
4
Rep Power
0

## Re: Two ways of summing a total, two different results!

Whoops, sorry. "winnings" is actually float. payPerCombo[], win, and win2 are int.

Still, I don't know why the sums differ.  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Two ways of summing a total, two different results!

I am not seeing anything. It could be related to float. How are you incrementing i and j in the first loop. In the second i is going from 0 to 33 inclusive. Also which loop runs a few million times? The first ? Are you reinitializing payPerCombo? Can you show some more of the code?

Regards,
Jim  Reply With Quote

5. ## Re: Two ways of summing a total, two different results!

A cause for numerical instability may be (I'm not saying this is the cause here) adding floating point number from largest to smallest (in magnitude) instead of vice versa. Integers were the invention of the gods, humans made those floating point numbers ;-)

kind regards,

Jos
Last edited by JosAH; 04-17-2013 at 09:26 PM. Reason: stupid me; who? me? yep, me.  Reply With Quote

6. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Two ways of summing a total, two different results!

For the sake of discussion, assume dollars. Why not just use int or long and work in cents? Then divide by 100 when displaying the amount as dollars (or whatever is appropriate for your locale). Or use BigDecimal. Both are gleaned from Essential Java, 2nd Ed. by Joshua Block.

Regards,
Jim  Reply With Quote

7. Member Join Date
Apr 2013
Posts
4
Rep Power
0

## Re: Two ways of summing a total, two different results!

i and j don't increment in the first loop. Those values are looked up (millions of times), and are always between 0-33.

No, I'm not re-initializing payPerCombo. I'm not using payPerCombo elsewhere, either. In any event, payPerCombo is giving me the *correct* total. It's the "winnings" variable which has the wrong total for some reason.

The loop is just running some million times:

Java Code:
```for (counter=1; counter<33000000; counter++) {
[lots of unrelated code omitted here]

if (win >= win2)  {payPerCombo[i] = payPerCombo[i]+win;  winnings+=win;}
else              {payPerCombo[j] = payPerCombo[j]+win2; winnings+=win2;}
}```
Last edited by michaelbluejay; 04-18-2013 at 05:22 AM.  Reply With Quote

8. Member Join Date
Apr 2013
Posts
4
Rep Power
0

## Re: Two ways of summing a total, two different results!

jim829, I'm not following you about dollars, cents, and BigDecimal. My problem is that the sums differ from each other. Is what you're saying related to that?  Reply With Quote

9. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Two ways of summing a total, two different results!

You are summing up your values in two different ways and I believe that is the problem. In one case, winnings is incremented regardless of the value of win or win2. In the other case, winnings is the sum of two different sums.

This may demonstrate what I believe to be the problem (related to what Jos said earlier).

Note: I edited this in an effort to better imitate the OP's actual code. Also, if the floats are changed to longs, the totals are equal.

Java Code:
```public class PrecisonProblem {
public static void main(String[] args) {
float a = 0;
float b = 0;
float total1 = 0;
float total2 = 0;
int win;
int win2;
for (int j = 0; j < 32*32*32*32*32; j++) {
win =  (int)(Math.random()*10);
win2 = (int)(Math.random()*10);
if (win >= win2) {
a += win;
total1 += win;
}
else {
b += win2;
total1 += win2;
}
}
total2 += a;
total2 += b;
System.out.println("total1 = " + total1);
System.out.println("total2 = " + total2);
}
}```
Regards,
Jim
Last edited by jim829; 04-18-2013 at 12:22 AM. Reason: Better example  Reply With Quote

float, precision 