Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default 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?

    Thank you very much for any help you're able to provide.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,946
    Rep Power
    4

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default 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.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,946
    Rep Power
    4

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default 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.

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

    Default 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?

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,946
    Rep Power
    4

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 14
    Last Post: 11-07-2011, 05:25 AM
  2. Summing a Series: Is this correct??
    By son012189 in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 05:36 PM
  3. Summing the digit
    By gozuhair in forum New To Java
    Replies: 13
    Last Post: 07-18-2011, 07:09 AM
  4. summing value within Hashtable
    By dc0m in forum New To Java
    Replies: 4
    Last Post: 09-23-2010, 12:17 AM
  5. Help with summing series
    By xplsivo in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 07:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •