Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
1da047bd
Commit
1da047bd
authored
Jan 28, 2015
by
Adam Palay
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix tolerance rounding error (TNL-904)
parent
0db8bac7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
1 deletions
+42
-1
common/lib/capa/capa/tests/test_util.py
+23
-0
common/lib/capa/capa/util.py
+19
-1
No files found.
common/lib/capa/capa/tests/test_util.py
View file @
1da047bd
...
...
@@ -81,6 +81,29 @@ class UtilTest(unittest.TestCase):
self
.
assertFalse
(
result
)
result
=
compare_with_tolerance
(
infinity
,
infinity
,
'1.0'
,
False
)
self
.
assertTrue
(
result
)
# Test absolute tolerance for smaller values
result
=
compare_with_tolerance
(
100.01
,
100.0
,
0.01
,
False
)
self
.
assertTrue
(
result
)
result
=
compare_with_tolerance
(
100.001
,
100.0
,
0.001
,
False
)
self
.
assertTrue
(
result
)
result
=
compare_with_tolerance
(
100.01
,
100.0
,
'0.01
%
'
,
False
)
self
.
assertTrue
(
result
)
result
=
compare_with_tolerance
(
100.002
,
100.0
,
0.001
,
False
)
self
.
assertFalse
(
result
)
result
=
compare_with_tolerance
(
0.4
,
0.44
,
0.01
,
False
)
self
.
assertFalse
(
result
)
result
=
compare_with_tolerance
(
100.01
,
100.0
,
0.010
,
False
)
self
.
assertTrue
(
result
)
# Test complex_number instructor_complex
result
=
compare_with_tolerance
(
0.4
,
complex
(
0.44
,
0
),
0.01
,
False
)
self
.
assertFalse
(
result
)
result
=
compare_with_tolerance
(
100.01
,
complex
(
100.0
,
0
),
0.010
,
False
)
self
.
assertTrue
(
result
)
result
=
compare_with_tolerance
(
110.1
,
complex
(
100.0
,
0
),
'10.0'
,
False
)
self
.
assertFalse
(
result
)
result
=
compare_with_tolerance
(
111.0
,
complex
(
100.0
,
0
),
'10
%
'
,
True
)
self
.
assertTrue
(
result
)
def
test_sanitize_html
(
self
):
"""
...
...
common/lib/capa/capa/util.py
View file @
1da047bd
...
...
@@ -2,9 +2,10 @@
Utility functions for capa.
"""
import
bleach
from
decimal
import
Decimal
from
calc
import
evaluator
from
cmath
import
isinf
from
cmath
import
isinf
,
isnan
#-----------------------------------------------------------------------------
#
# Utility functions used in CAPA responsetypes
...
...
@@ -64,6 +65,23 @@ def compare_with_tolerance(student_complex, instructor_complex, tolerance=defaul
# `tolerance` both equal to infinity. Then, below we would have
# `inf <= inf` which is a fail. Instead, compare directly.
return
student_complex
==
instructor_complex
# because student_complex and instructor_complex are not necessarily
# complex here, we enforce it here:
student_complex
=
complex
(
student_complex
)
instructor_complex
=
complex
(
instructor_complex
)
# if both the instructor and student input are real,
# compare them as Decimals to avoid rounding errors
if
not
(
instructor_complex
.
imag
or
student_complex
.
imag
):
# if either of these are not a number, short circuit and return False
if
isnan
(
instructor_complex
.
real
)
or
isnan
(
student_complex
.
real
):
return
False
student_decimal
=
Decimal
(
str
(
student_complex
.
real
))
instructor_decimal
=
Decimal
(
str
(
instructor_complex
.
real
))
tolerance_decimal
=
Decimal
(
str
(
tolerance
))
return
abs
(
student_decimal
-
instructor_decimal
)
<=
tolerance_decimal
else
:
# v1 and v2 are, in general, complex numbers:
# there are some notes about backward compatibility issue: see responsetypes.get_staff_ans()).
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment