Skip to content

Commit

Permalink
Fix compute_correct_average to do float division and rounding instead…
Browse files Browse the repository at this point in the history
… of integer division.

@viroulep noticed this bug in
#2984 (comment).
  • Loading branch information
jfly committed Jul 16, 2018
1 parent d699a8f commit 19205f3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion WcaOnRails/app/models/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def compute_correct_average
100 * sum_moves / counting_solve_times.length
else
sum_centis = counting_solve_times.sum(&:time_centiseconds)
sum_centis / counting_solve_times.length
(sum_centis.to_f / counting_solve_times.length).round
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions WcaOnRails/spec/models/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@
expect(result).to be_invalid_with_errors(average: ["should be 43"])
end

it "rounds instead of truncates" do
result = build_result(value1: 4, value2: 4, value3: 3, value4: 0, value5: 0, best: 3, average: 4)
expect(result).to be_valid

result.average = 33
expect(result.compute_correct_average).to eq 4
expect(result).to be_invalid_with_errors(average: ["should be 4"])
end

it "missing solves" do
result = build_result(value1: 42, value2: 0, value3: 0, value4: 0, value5: 0, best: 42, average: 0)
expect(result.average_is_not_computable_reason).to be_truthy
Expand Down

0 comments on commit 19205f3

Please sign in to comment.