Results 1 to 17 of 17
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default retrieving percentages

    i am trying to get the percentage of votes for each candidate for my program.
    votes is an array (with # of votes for each candidate, total is the total number of votes, index shows me how many candidates there are (so i only run the loop "i<index" times), and percents is the initialized array.

    Java Code:
    percents = percentages(votes, total, index, percents);
    this is my method to get each of the percents:

    Java Code:
    public static int[] percentages(int votes[], int total, int index, int percents[]) {
    	
    		int i = 0;
    	
    		for (i=0; i<index; i++) {
    			percents[i] = (votes[i]/total);
    		}
    	
    	return percents;
    	
    	}
    after printing out the percents, i get 0 for each one.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Dividing an int by and int returns an int. I assume that votes[i]/total gets you a number between 0 and 1. If you store that as an int, it's going to truncate and store zero.

    You probably want to use a double instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Integer division seems to be the culprit here. Since the calculated percent will be a number >=0 && < 1, the decimal will be truncated. To get around integer division you will have to cast one, or both values to a double. You will also have to return a double array.

    Java Code:
    int i = 10;
    int j = 15
    j / i == 1;
    (double)j / i == 1.5;
    (double)j / (double)i == 1.5;
    edit: too slow :(

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by sunde887 View Post
    Integer division seems to be the culprit here. Since the calculated percent will be a number >=0 && < 1, the decimal will be truncated. To get around integer division you will have to cast one, or both values to a double. You will also have to return a double array.

    Java Code:
    int i = 10;
    int j = 15
    j / i == 1;
    (double)j / i == 1.5;
    (double)j / (double)i == 1.5;
    edit: too slow :(
    Also, if you're going to be checking for equality, I'd give this a read-through: What Every Computer Scientist Should Know About Floating-Point Arithmetic
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by KevinWorkman View Post
    Also, if you're going to be checking for equality, I'd give this a read-through: What Every Computer Scientist Should Know About Floating-Point Arithmetic
    That looks like a lengthy read. I'll be sure to be checking that out throughout the next few days.

    When explaining stuff to people I usually use == to convey equality instead of =, I know = would do fine, but I figure, in case they copy/paste I already used the correct version.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by sunde887 View Post
    That looks like a lengthy read. I'll be sure to be checking that out throughout the next few days.
    Well, here's the gist:

    Floating point arithmetic is rarely exact. While some numbers, such as 0.5, can be exactly represented as a binary (base 2) decimal (since 0.5 equals 2^-1), other numbers, such as 0.1, cannot be. As a result, floating point operations may result in rounding errors, yielding a result that is close to -- but not equal to -- the result you might expect. For example, the simple calculation below results in 2.600000000000001, rather than 2.6:
    Java Code:
      double s=0;
    
      for (int i=0; i<26; i++)
        s += 0.1;
      System.out.println(s);
    Source: Java performance tuning tips

    Quote Originally Posted by sunde887 View Post
    When explaining stuff to people I usually use == to convey equality instead of =, I know = would do fine, but I figure, in case they copy/paste I already used the correct version.
    I'm not sure I follow. Single '=' is assignment. Double "==" checks for equality.
    Last edited by KevinWorkman; 04-19-2011 at 02:35 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Sorry about the confusion, it just seems natural to use a single = when generally typing, but to programmers you use == so when I'm saying something equals another I just tend to stick to == always now instead. I'm just being confusing. Thanks for the summary of the thread. I hope the op finds all this helpful as well.

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

    Default

    Quote Originally Posted by sunde887 View Post
    Sorry about the confusion, it just seems natural to use a single = when generally typing, but to programmers you use == so when I'm saying something equals another I just tend to stick to == always now instead. I'm just being confusing. Thanks for the summary of the thread. I hope the op finds all this helpful as well.
    Better read that article; chances are higher that'll understand that, say, the following doesn't do what you naively might expect it to do:

    Java Code:
    public class T {
    	public static void main(String[] args) {
    		for (double x= 0.1; x != 1; x+= 0.1)
    			System.out.println(x);
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    here is the data file that i read:

    Java Code:
    Smith  80,000
    Jones   100,000
    Scott   75,000
    Washington   110,000
    Duffy   125,000
    Jacobs   67,000
    i get the total of all the numbers, then take each number, and divide by the total to get my percentage. i am getting 0 for some reason. i guess my array is empty?

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    A percentage will result in a number between 0 and 1. It's been explained that you should use a double instead of an int. Also don't forget that double values must have a 1.0 to produce the decimal.

    Also the article posted is worth the read so your code does not have miscalculations.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    so i updated my method to doubles, and here is what i have:

    Java Code:
    public static double[] percentages(int votes[], int total, int index, double percents[]) {
    	
    		int i = 0;
    	
    		for (i=0; i<index; i++) {
    			percents[i] = (votes[i]/total);
    		}
    	
    	return percents;
    	
    	}
    i still get 0's though.

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

    Default

    Quote Originally Posted by droidus View Post
    so i updated my method to doubles, and here is what i have:

    Java Code:
    public static double[] percentages(int votes[], int total, int index, double percents[]) {
    	
    		int i = 0;
    	
    		for (i=0; i<index; i++) {
    			percents[i] = (votes[i]/total);
    		}
    	
    	return percents;
    	
    	}
    i still get 0's though.
    votes[ i ] and total are still int typed values so the division will be an int divistion. You did read your text book did you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    My first post explains the problem, and shows you the change to make. Just in case, you should be casting to double.

  14. #14
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    ok. so i have a bunch of 0's after the decimal when i print out the values now. how do i get rid of these? can i use printf?

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by droidus View Post
    ok. so i have a bunch of 0's after the decimal when i print out the values now. how do i get rid of these? can i use printf?
    What happened when you tried? Also look into DecimalFormat.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    got it: System.out.printf("%,.0f", total);

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped. Please mark your thread solved with the thread tools at the top.

Similar Threads

  1. Percentages
    By Angziety94 in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 05:14 AM
  2. using percentages?
    By shroomiin in forum New To Java
    Replies: 3
    Last Post: 09-30-2009, 01:58 AM
  3. [SOLVED] Noobie - Calculating Percentages
    By fullmetaljacket in forum New To Java
    Replies: 16
    Last Post: 05-22-2009, 01:10 AM
  4. Percentages
    By Ciwan in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 05:34 AM
  5. Qadratic Formula and Percentages
    By bbtgirl in forum JCreator
    Replies: 4
    Last Post: 02-07-2009, 03:07 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
  •