Results 1 to 17 of 17
Thread: retrieving percentages
- 04-19-2011, 01:54 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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.
this is my method to get each of the percents:Java Code:percents = percentages(votes, total, index, percents);
after printing out the percents, i get 0 for each one.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; }
- 04-19-2011, 02:07 PM #2
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!
- 04-19-2011, 02:11 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
edit: too slow :(Java Code:int i = 10; int j = 15 j / i == 1; (double)j / i == 1.5; (double)j / (double)i == 1.5;
- 04-19-2011, 02:13 PM #4
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!
- 04-19-2011, 02:17 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-19-2011, 02:27 PM #6
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:
Source: Java performance tuning tipsJava Code:double s=0; for (int i=0; i<26; i++) s += 0.1; System.out.println(s);
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!
- 04-19-2011, 02:38 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-19-2011, 02:57 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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); } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-19-2011, 07:12 PM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
here is the data file that i read:
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?Java Code:Smith 80,000 Jones 100,000 Scott 75,000 Washington 110,000 Duffy 125,000 Jacobs 67,000
- 04-19-2011, 07:20 PM #10
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.
- 04-20-2011, 12:12 PM #11
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
so i updated my method to doubles, and here is what i have:
i still get 0's though.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; }
- 04-20-2011, 12:18 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 04-20-2011, 12:49 PM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
My first post explains the problem, and shows you the change to make. Just in case, you should be casting to double.
- 04-20-2011, 02:12 PM #14
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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?
- 04-20-2011, 02:15 PM #15
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-20-2011, 02:24 PM #16
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
got it: System.out.printf("%,.0f", total);
- 04-20-2011, 02:26 PM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Percentages
By Angziety94 in forum New To JavaReplies: 1Last Post: 12-09-2010, 05:14 AM -
using percentages?
By shroomiin in forum New To JavaReplies: 3Last Post: 09-30-2009, 01:58 AM -
[SOLVED] Noobie - Calculating Percentages
By fullmetaljacket in forum New To JavaReplies: 16Last Post: 05-22-2009, 01:10 AM -
Percentages
By Ciwan in forum New To JavaReplies: 3Last Post: 02-16-2009, 05:34 AM -
Qadratic Formula and Percentages
By bbtgirl in forum JCreatorReplies: 4Last Post: 02-07-2009, 03:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks