Commit a2abd868 authored by Marius Renner's avatar Marius Renner

fix rounding error in ScoreBasedScoreCalculator

Cast int to double before division to avoid truncation of the division
result.
Changed corresponding test so that it fails with the old implementation.
parent cd570c65
......@@ -48,7 +48,7 @@ public class ScoreBasedScoreCalculator extends VariantScoreCalculator {
if (courseMaximumValue == 0 || numUsers == 0) {
return 0;
}
final double courseAverageValue = userTotalValue / numUsers;
final double courseAverageValue = (double)userTotalValue / numUsers;
final double courseProgress = courseAverageValue / courseMaximumValue;
return (int) Math.min(100, Math.round(courseProgress * 100));
}
......
......@@ -91,18 +91,23 @@ public class ScoreBasedScoreCalculatorTest {
String q3 = this.addQuestion("lecture", 100);
User u1 = new TestUser("user1");
User u2 = new TestUser("user2");
User u3 = new TestUser("user3");
// Both users achieve 200 points
this.addAnswer(q1, u1, 100);
this.addAnswer(q1, u2, 100);
this.addAnswer(q1, u3, 0);
this.addAnswer(q2, u1, 0);
this.addAnswer(q2, u2, 0);
this.addAnswer(q2, u2, 100);
this.addAnswer(q2, u3, 0);
this.addAnswer(q3, u1, 100);
this.addAnswer(q3, u2, 100);
this.addAnswer(q3, u3, 0);
lp.setQuestionVariant("lecture");
ScoreStatistics u1LectureProgress = lp.getMyProgress(null, u1);
// 200 / 300 = 0,67
assertEquals(67, u1LectureProgress.getCourseProgress());
// (500/3) / 300 ~= 0,56.
assertEquals(56, u1LectureProgress.getCourseProgress());
// 200 / 300 ~= 0,67.
assertEquals(67, u1LectureProgress.getMyProgress());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment