From 587221e394382b69b5bdf46e6987bbeb827f44b5 Mon Sep 17 00:00:00 2001 From: Pasit Sangprachathanarak Date: Sun, 1 Nov 2026 21:13:43 +0800 Subject: [PATCH] Fix score precision rounding Round aggregate, public, and maximum scores to the configured precision so floating-point accumulation errors do not cause score comparisons to differ. --- cms/grading/scoretypes/Sum.py | 3 +++ cms/grading/scoretypes/abc.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/cms/grading/scoretypes/Sum.py b/cms/grading/scoretypes/Sum.py index 1a7a6fbb13..e685d0dd48 100644 --- a/cms/grading/scoretypes/Sum.py +++ b/cms/grading/scoretypes/Sum.py @@ -185,6 +185,9 @@ def compute_score(self, submission_result): else: public_testcases.append({"idx": idx}) + score = round(score, self.score_precision) + public_score = round(public_score, self.score_precision) + return score, testcases, public_score, public_testcases, [] def get_public_outcome(self, outcome: float) -> str: diff --git a/cms/grading/scoretypes/abc.py b/cms/grading/scoretypes/abc.py index ad02038316..b5c59e9762 100644 --- a/cms/grading/scoretypes/abc.py +++ b/cms/grading/scoretypes/abc.py @@ -85,6 +85,10 @@ def __init__( self.max_score, self.max_public_score, self.ranking_headers = ( self.max_scores() ) + # Sums of floats may accumulate errors (e.g. 0.1 + 0.2); round + # so that they compare equal to the (rounded) submission scores. + self.max_score = round(self.max_score, score_precision) + self.max_public_score = round(self.max_public_score, score_precision) except Exception as e: raise ValueError( "Unable to instantiate score type (probably due to invalid " @@ -639,6 +643,7 @@ def compute_score(self, submission_result): # are rounded. However we are using floats not Decimals # and this can cause errors. So we round again to be sure. score = round(score, score_precision) + public_score = round(public_score, score_precision) return score, subtasks, public_score, public_subtasks, ranking_details