Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
1da047bd
Commit
1da047bd
authored
10 years ago
by
Adam Palay
Browse files
Options
Downloads
Patches
Plain Diff
fix tolerance rounding error (TNL-904)
parent
0db8bac7
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/lib/capa/capa/tests/test_util.py
+23
-0
23 additions, 0 deletions
common/lib/capa/capa/tests/test_util.py
common/lib/capa/capa/util.py
+19
-1
19 additions, 1 deletion
common/lib/capa/capa/util.py
with
42 additions
and
1 deletion
common/lib/capa/capa/tests/test_util.py
+
23
−
0
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
):
"""
...
...
This diff is collapsed.
Click to expand it.
common/lib/capa/capa/util.py
+
19
−
1
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()).
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment