Fix bug when excluding candidate with 0 votes

This commit is contained in:
RunasSudo 2021-05-30 18:27:28 +10:00
parent a595f2ff6b
commit 5189a74010
No known key found for this signature in database
GPG Key ID: 7234E476BF21C61A
1 changed files with 46 additions and 42 deletions

View File

@ -445,53 +445,57 @@ where
panic!("Invalid --exclusion"); panic!("Invalid --exclusion");
} }
let value = &votes[0].value / &votes[0].ballot.orig_value;
// Count next preferences
let result = next_preferences(state, votes);
if opts.exclusion == "one_round" {
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes.", result.total_ballots, result.total_votes));
} else if opts.exclusion == "by_value" {
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes, received at value {:.2}.", result.total_ballots, result.total_votes, value));
}
// Transfer candidate votes
let mut checksum = N::new(); let mut checksum = N::new();
for (candidate, entry) in result.candidates.into_iter() { if votes.len() > 0 {
let parcel = entry.votes as Parcel<N>; let value = &votes[0].value / &votes[0].ballot.orig_value;
let count_card = state.candidates.get_mut(candidate).unwrap();
count_card.parcels.push(parcel);
// Round transfers // Count next preferences
let mut candidate_transfers = entry.num_votes; let result = next_preferences(state, votes);
if let Some(dps) = opts.round_votes {
candidate_transfers.floor_mut(dps); if opts.exclusion == "one_round" {
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes.", result.total_ballots, result.total_votes));
} else if opts.exclusion == "by_value" {
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes, received at value {:.2}.", result.total_ballots, result.total_votes, value));
} }
count_card.transfer(&candidate_transfers);
checksum += candidate_transfers;
}
// Transfer exhausted votes
let parcel = result.exhausted.votes as Parcel<N>;
state.exhausted.parcels.push(parcel);
let mut exhausted_transfers = result.exhausted.num_votes;
if let Some(dps) = opts.round_votes {
exhausted_transfers.floor_mut(dps);
}
state.exhausted.transfer(&exhausted_transfers);
checksum += exhausted_transfers;
if votes_remaining > 0 {
// Subtract from candidate tally
let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
checksum -= &result.total_votes;
count_card.transfer(&-result.total_votes);
// By definition, there is no loss by fraction // Transfer candidate votes
} else { for (candidate, entry) in result.candidates.into_iter() {
let parcel = entry.votes as Parcel<N>;
let count_card = state.candidates.get_mut(candidate).unwrap();
count_card.parcels.push(parcel);
// Round transfers
let mut candidate_transfers = entry.num_votes;
if let Some(dps) = opts.round_votes {
candidate_transfers.floor_mut(dps);
}
count_card.transfer(&candidate_transfers);
checksum += candidate_transfers;
}
// Transfer exhausted votes
let parcel = result.exhausted.votes as Parcel<N>;
state.exhausted.parcels.push(parcel);
let mut exhausted_transfers = result.exhausted.num_votes;
if let Some(dps) = opts.round_votes {
exhausted_transfers.floor_mut(dps);
}
state.exhausted.transfer(&exhausted_transfers);
checksum += exhausted_transfers;
if votes_remaining > 0 {
// Subtract from candidate tally
let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
checksum -= &result.total_votes;
count_card.transfer(&-result.total_votes);
// By definition, there is no loss by fraction
}
}
if votes_remaining == 0 {
// Finalise candidate votes // Finalise candidate votes
let count_card = state.candidates.get_mut(excluded_candidate).unwrap(); let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
checksum -= &count_card.votes; checksum -= &count_card.votes;