All Good Things Must Come To An End

As you all know, I have been doing WordPress plugins and supporting it for the past 6 years. These 6 years of my life, I have been through my polytechnic education, my national service as well as my university education.

I just graduated from university in December 2009 and have been looking for full-time jobs. I am offered a full-time job and will be starting work on 1st February 2010.

I regret to say that I am NOT ABLE to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

However, I WILL still update my plugins whenever I can and you still can report bugs to me via email and I will try to fix it.

Author Topic: Wrong Percentage Calculation in 2.50  (Read 2716 times)

0 Members and 1 Guest are viewing this topic.

Offline Ghandi

  • Newbie
  • *
  • Posts: 2
    • View Profile
Wrong Percentage Calculation in 2.50
« on: 17 June 2009, 12:08 »
I have some polls where the users can select multiple answers. Since 2.50 I noticed that the last percentage value is always negative. I checked the source and found this:

Code: [Select]
// Make Sure That Total Percentage Is 100% By Adding A Buffer To The Last Poll Answer
$poll_answer_percentage_array[] = $poll_answer_percentage;
if(sizeof($poll_answer_percentage_array) == sizeof($poll_answers)) {
$percentage_error_buffer = 100 - array_sum($poll_answer_percentage_array);
$poll_answer_percentage = $poll_answer_percentage + $percentage_error_buffer;
}

This works well on single answer polls. On mutiple answer polls you can get negative values if the total percentage is greater 100.

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #1 on: 17 June 2009, 13:30 »
Thanks for the bug report.

Can you replace the chunk of code you pasted with:
Code: [Select]
if($poll_multiple_ans == 0) {
$poll_answer_percentage_array[] = $poll_answer_percentage;
if(sizeof($poll_answer_percentage_array) == sizeof($poll_answers)) {
$percentage_error_buffer = 100 - array_sum($poll_answer_percentage_array);
$poll_answer_percentage = $poll_answer_percentage + $percentage_error_buffer;
}
}

It should fix the bug. If it works ok for you, I will commit it.

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Ghandi

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #2 on: 17 June 2009, 13:39 »
It should fix the bug. If it works ok for you, I will commit it.

It works great, thanks :).

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline infected

  • Newbie
  • *
  • Posts: 9
    • View Profile
  • WordPress Version: WordPress 2.9.x
Re: Wrong Percentage Calculation in 2.50
« Reply #4 on: 17 June 2009, 16:23 »
Thanks for the fix! Works fine!

Michael

Offline Sheamus

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #5 on: 21 June 2009, 18:13 »
I have the same problem with the percentages, but when I attempt this fix, the plugin won't reactivate due to a fatal error. I've tried a couple of times now.

I use WP 2.8. The plugin works fine otherwise, and has done for some time. It just doesn't add up the percentages right when users can provide multiple answers.

Example: http://twittercism.com/poll-why-block/

I know you're a very busy chap, but any help is most appreciated. :)

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #6 on: 22 June 2009, 01:00 »
Could you re-download the plugin?

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Sheamus

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #7 on: 22 June 2009, 08:35 »
Yeah, that fixed it - thank you! :)

Offline Steve Johnson

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #8 on: 30 June 2009, 02:32 »
This is still happening for me with a download that I performed on 2009-06-25. I checked the code and it appears to contain the fix, but the percentages are still wrong. This is pasted directly from the code on my site:

Code: [Select]
// Make Sure That Total Percentage Is 100% By Adding A Buffer To The Last Poll Answer
if($poll_multiple_ans == 0) {
$poll_answer_percentage_array[] = $poll_answer_percentage;
if(sizeof($poll_answer_percentage_array) == sizeof($poll_answers)) {
$percentage_error_buffer = 100 - array_sum($poll_answer_percentage_array);
$poll_answer_percentage = $poll_answer_percentage + $percentage_error_buffer;
}
}

Here are the polls displaying the wrong final percentages: http://www.blog.cadnauseam.com/pollsarchive/ - scroll down to see this:

Code: [Select]
Which of these is most evil? (Choose up to 3).

    * Apple (19%, 74 Votes)
    * Autodesk (22%, 86 Votes)
    * Dell (7%, 27 Votes)
    * Disney (18%, 71 Votes)
    * Gaahl (4%, 17 Votes)
    * Google (7%, 26 Votes)
    * Microsoft (36%, 138 Votes)
    * Miley Cyrus (22%, 84 Votes)
    * RIAA/IFPI/MPAA (33%, 129 Votes)
    * Satan (40%, 153 Votes)
    * Sony (5%, 21 Votes)
    * Steve Johnson (3%, 13 Votes)
    * The Pirate Bay (-116%, 24 Votes)

Total Voters: 386

It's also wrong in non-multiple-selection polls, but not quite so obviously:

Code: [Select]
AutoCAD 2009 users: pull-down menus - is your menu bar turned on (MENUBAR=1)?

    * Yes, it's on all the time (68%, 136 Votes)
    * Yes, it's usually on but I sometimes turn it off (7%, 13 Votes)
    * Yes in verticals, no in AutoCAD (3%, 5 Votes)
    * Yes (other) (2%, 4 Votes)
    * No, it's usually off but I sometimes turn it on (2%, 4 Votes)
    * No, I use the menu under the big red A (7%, 13 Votes)
    * No, I never use pull-downs (10%, 19 Votes)
    * No (other) (1%, 5 Votes)

Total Voters: 199

See the last item: 5 votes is 1% (it's actually just over 2.5%). See above: 5 votes is 3%, 4 votes is 2%.

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #9 on: 30 June 2009, 02:41 »
Okie, so it is only in the poll archive am I right? I will check it.

The percentage will definitely be not accurate as I have to forgo some of it so that the total adds up to 100% (refering to the last poll)

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Steve Johnson

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #10 on: 30 June 2009, 08:28 »
Yes, I just checked and it appears to be only in the archive. In the sidebar:

Code: [Select]
    * David Harrington (50%, 1 Votes)
    * Mark Kiker (50%, 1 Votes)
    * David Kingsley (0%, 0 Votes)
    * Kenneth Leary (50%, 1 Votes)
    * Marv Muston (0%, 0 Votes)
    * Dario Passariello (0%, 0 Votes)
    * Jane Smith (100%, 2 Votes)

Total Voters: 2

In archive:

Code: [Select]
    * David Harrington (50%, 1 Votes)
    * Mark Kiker (50%, 1 Votes)
    * David Kingsley (0%, 0 Votes)
    * Kenneth Leary (50%, 1 Votes)
    * Marv Muston (0%, 0 Votes)
    * Dario Passariello (0%, 0 Votes)
    * Jane Smith (-50%, 2 Votes)

Total Voters: 2

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #11 on: 30 June 2009, 08:32 »
Okie will fix it

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Steve Johnson

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #12 on: 30 June 2009, 08:32 »
Thanks! Just a thought for future consideration: wouldn't adding a decimal place of accuracy (x.x%) make the cumulative errors much smaller?

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #13 on: 30 June 2009, 08:38 »
it was 2 decimal at first, but some people complain it is too much, then i make it whole number. But most polls only use whole number which is true.

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #14 on: 30 June 2009, 10:46 »
Could you replace wp-polls.php with http://plugins.trac.wordpress.org/export/130751/wp-polls/tags/2.50/wp-polls.php to see if it works?

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Steve Johnson

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #15 on: 01 July 2009, 01:34 »
That's much better on the multiple selection polls, thank you very much. It still has the 5/199 = 1% issue, I'm not sure how that could be addressed.

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #16 on: 01 July 2009, 01:40 »
That can't be help, I have to tally it to make it 100% so the last poll answer will suffer the buffer.

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Steve Johnson

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #17 on: 01 July 2009, 06:48 »
Actually, I'm not sure you do have to make it add up to 100%. Personally, I'd prefer each percentage figure to be as accurate as the precision allows. I think that would be better than just nobbling the last item in the list to make the percentages add up to 100.

If I put the numbers from one of my polls into Excel and get it to calculate the percentages and sum them, I get this kind of thing (3 different precisions shown):

Code: [Select]
136  68%    68.3%   68.34%
13    7%     6.5%    6.53%
5     3%     2.5%    2.51%
4     2%     2.0%    2.01%
4     2%     2.0%    2.01%
13    7%     6.5%    6.53%
19    10%    9.5%    9.55%
5     3%     2.5%    2.51%

199  100%  100.0%  100.00%

Excel is, of course, summing the actual calculated values to get to 100%, not the rounded displayed values. If I add up the rounded displayed percentages, I get 102%, 99.8%, 99.99%.

If I were to see an Excel table like the above, I would consider it perfectly valid. I know that if individual rounded percentages are added up then this might not add up to 100%, and don't see a problem with it. While it's possible that a blog reader might go to the trouble of adding up the displayed values and complaining that they don't add up to 100%, It's certainly a less obvious problem than 5 votes being shown as 3% on one item and 1% on another.

I'm just providing my own perspective for your information, I don't expect you to write your code to my specifications. :) Thank you for providing this excellent tool free of charge.

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Wrong Percentage Calculation in 2.50
« Reply #18 on: 01 July 2009, 07:12 »
I got what you mean, because I have to round it up first before the buffer, if I have 2 decimal place, it may not add up to 100% as well lol some user will get 99.9%

To cater to your need, you can remove the buffer, line 631 to 638 or wp-polls.php

Code: [Select]
if($poll_multiple_ans == 0) {
$poll_answer_percentage_array[] = $poll_answer_percentage;
if(sizeof($poll_answer_percentage_array) == sizeof($poll_answers)) {
$percentage_error_buffer = 100 - array_sum($poll_answer_percentage_array);
$poll_answer_percentage = $poll_answer_percentage + $percentage_error_buffer;
}
}

Modify the rounding,

Line 618
Code: [Select]
$poll_answer_percentage = round((($poll_answer_votes/$poll_question_totalvoters)*100));To
Code: [Select]
$poll_answer_percentage = round((($poll_answer_votes/$poll_question_totalvoters)*100), 2);

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Steve Johnson

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Wrong Percentage Calculation in 2.50
« Reply #19 on: 01 July 2009, 11:47 »
Cool! I found the other bits of similar code so it does it both in the sidebar and the archive. Brilliant, all done, thank you so much!