* Get a data of given precision, assuring the sum of percentages * in valueList is 1. * The largest remainer method is used. * https://en.wikipedia.org/wiki/Largest_remainder_method * * @param valueList a list of all data * @param idx index of the data to be processed i
(valueList, idx, precision)
| 6754 | */ |
| 6755 | |
| 6756 | function getPercentWithPrecision(valueList, idx, precision) { |
| 6757 | if (!valueList[idx]) { |
| 6758 | return 0; |
| 6759 | } |
| 6760 | |
| 6761 | var sum = reduce(valueList, function (acc, val) { |
| 6762 | return acc + (isNaN(val) ? 0 : val); |
| 6763 | }, 0); |
| 6764 | |
| 6765 | if (sum === 0) { |
| 6766 | return 0; |
| 6767 | } |
| 6768 | |
| 6769 | var digits = Math.pow(10, precision); |
| 6770 | var votesPerQuota = map(valueList, function (val) { |
| 6771 | return (isNaN(val) ? 0 : val) / sum * digits * 100; |
| 6772 | }); |
| 6773 | var targetSeats = digits * 100; |
| 6774 | var seats = map(votesPerQuota, function (votes) { |
| 6775 | // Assign automatic seats. |
| 6776 | return Math.floor(votes); |
| 6777 | }); |
| 6778 | var currentSum = reduce(seats, function (acc, val) { |
| 6779 | return acc + val; |
| 6780 | }, 0); |
| 6781 | var remainder = map(votesPerQuota, function (votes, idx) { |
| 6782 | return votes - seats[idx]; |
| 6783 | }); // Has remainding votes. |
| 6784 | |
| 6785 | while (currentSum < targetSeats) { |
| 6786 | // Find next largest remainder. |
| 6787 | var max = Number.NEGATIVE_INFINITY; |
| 6788 | var maxId = null; |
| 6789 | |
| 6790 | for (var i = 0, len = remainder.length; i < len; ++i) { |
| 6791 | if (remainder[i] > max) { |
| 6792 | max = remainder[i]; |
| 6793 | maxId = i; |
| 6794 | } |
| 6795 | } // Add a vote to max remainder. |
| 6796 | |
| 6797 | |
| 6798 | ++seats[maxId]; |
| 6799 | remainder[maxId] = 0; |
| 6800 | ++currentSum; |
| 6801 | } |
| 6802 | |
| 6803 | return seats[idx] / digits; |
| 6804 | } // Number.MAX_SAFE_INTEGER, ie do not support. |
| 6805 | |
| 6806 | var MAX_SAFE_INTEGER = 9007199254740991; |
| 6807 | /** |
no test coverage detected
searching dependent graphs…