Two ways of summing a total, two different results!

• 04-17-2013, 06:59 PM
michaelbluejay
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:

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?

Thank you very much for any help you're able to provide.
• 04-17-2013, 07:45 PM
JosAH
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
• 04-17-2013, 07:55 PM
michaelbluejay
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.
• 04-17-2013, 09:08 PM
jim829
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
• 04-17-2013, 09:21 PM
JosAH
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
• 04-17-2013, 09:29 PM
jim829
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
• 04-17-2013, 09:35 PM
michaelbluejay
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:

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;} }```
• 04-17-2013, 09:37 PM
michaelbluejay
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?
• 04-17-2013, 10:01 PM
jim829
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.

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