Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rounding Problem

    Heres my problem:
    I created a method that returns a handicap in integer form. Before you can return it, however, you must cast it as an int. My problem arises in some cases where the handicap returned is 1 too high. I'm wondering where my logic may be wrong in some cases; so far.. I can't find the problem.

    Code:
    public int seeHandicap ()
    {
          int highScore;
          float average = 0, sum = 0;
          double handicap;
          highScore = hiScore(); // returns highest score in array
          for ( int i = 0; i < SCORES; i++ )
             sum = sum + scores[i]; // adds all scores in array together
          sum -= highScore; // highest score is thrown out
          average = sum / ( SCORES - 1 ); // float = float/int (default float,right?)
          handicap = ( average - PAR ) * HANDICAP_P; // (equation for finding handicap)
          if ( handicap < 0 )
             return 0;
          return (int)handicap; // casts double handicap as an integer
    }
    thanks
    Last edited by ah288; 09-24-2007 at 02:16 AM.

  • #2
    Regular Coder
    Join Date
    May 2004
    Location
    New Hampshire, America
    Posts
    246
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by ah288 View Post
    Heres my problem:
    I created a method that returns a handicap in integer form. Before you can return it, however, you must cast it as an int. My problem arises in some cases where the handicap returned is 1 too high. I'm wondering where my logic may be wrong in some cases; so far.. I can't find the problem.

    Code:
    public int seeHandicap ()
    {
          int highScore;
          float average = 0, sum = 0;
          double handicap;
          highScore = hiScore(); // returns highest score in array
          for ( int i = 0; i < SCORES; i++ )
             sum = sum + scores[i]; // adds all scores in array together
          sum -= highScore; // highest score is thrown out
          average = su;m / ( SCORES - 1 ) // float = float/int (default float,right?)
          handicap = ( average - PAR ) * HANDICAP_P; // (equation for finding handicap)
          if ( handicap < 0 )
             return 0;
          return (int)handicap; // casts double handicap as an integer
    }
    thanks
    depending on your compiler, this might be the problem
    Change: "average = sum / ( SCORES - 1 );"
    To: "average = (float)(sum / (float)(SCORES - 1));"
    because the int causes the division by a float to result in an int, which is the most exact unit that it could result in.
    Your least precise unit of measurement or division cannot be less precise then the result.
    Last edited by Dunna; 10-04-2007 at 05:49 AM.


  •  

    Posting Permissions

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