Skip to content

Commit

Permalink
Use Decimal in floats checker
Browse files Browse the repository at this point in the history
  • Loading branch information
kiritofeng committed Sep 30, 2019
1 parent d700741 commit 8e4dbfd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions dmoj/checkers/floats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
from re import split as resplit

from dmoj.error import InternalError
Expand Down Expand Up @@ -27,7 +28,13 @@ def verify_default(process_float, judge_float, epsilon):
abs(1.0 - process_float / judge_float) <= epsilon)


def check(process_output, judge_output, precision=6, error_mode='default', **kwargs):
def check(process_output, judge_output, precision=6, fast=True, error_mode='default', **kwargs):

if fast:
float_datatype = float
else:
float_datatype = Decimal

# Discount empty lines
process_lines = list(filter(None, resplit(b'[\r\n]', utf8bytes(process_output))))
judge_lines = list(filter(None, resplit(b'[\r\n]', utf8bytes(judge_output))))
Expand All @@ -44,7 +51,7 @@ def check(process_output, judge_output, precision=6, error_mode='default', **kwa
if not verify_float:
raise InternalError('invalid `error_mode` value')

epsilon = 10 ** -int(precision)
epsilon = float_datatype(10) ** -int(precision)

try:
for process_line, judge_line in zip(process_lines, judge_lines):
Expand All @@ -57,13 +64,13 @@ def check(process_output, judge_output, precision=6, error_mode='default', **kwa
for process_token, judge_token in zip(process_tokens, judge_tokens):
# Allow mixed tokens, for lines like "abc 0.68 def 0.70"
try:
judge_float = float(judge_token)
judge_float = float_datatype(judge_token)
except ValueError:
# If it's not a float the token must match exactly
if process_token != judge_token:
return False
else:
process_float = float(process_token)
process_float = float_datatype(process_token)

if not verify_float(process_float, judge_float, epsilon):
return False
Expand Down

0 comments on commit 8e4dbfd

Please sign in to comment.