Skip to content

Commit 5ce94ad

Browse files
authored
Merge pull request #308 from mitodl/long_int_fix
Fixing bug with long ints
2 parents 6cd9cef + 121e872 commit 5ce94ad

File tree

7 files changed

+20
-5
lines changed

7 files changed

+20
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
A library of graders for edX Custom Response problems.
66

7-
Version 2.3.1 ([changelog](docs/changelog.md))
7+
Version 2.3.2 ([changelog](docs/changelog.md))
88

9-
Copyright 2017-2020 Jolyon Bloomfield and Chris Chudzicki
9+
Copyright 2017-2021 Jolyon Bloomfield and Chris Chudzicki
1010

1111
Licensed under the [BSD-3 License](LICENSE).
1212

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This is a major new version with many new features added. We have been very care
66

77
## Version 2.3
88

9+
### Version 2.3.2
10+
11+
* Fixed a bug where long integers passed in as a variable (via `DiscreteSet`) or user-defined constant would cause an error in evaluation (such numbers are now cast as floats).
12+
913
### Version 2.3.1
1014

1115
* Added a feature to math graders to remove default constants such as `pi` or `i`.

docs/grading_math/sampling.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ Sample from any discrete set of values that are specified in a tuple. A single v
230230
>>> sampler = DiscreteSet((1, 3, 5, 7, 9))
231231
>>> # Always select 3.5
232232
>>> sampler = DiscreteSet(3.5)
233+
>>> # This is equivalent to the above:
234+
>>> sampler = 3.5
233235
>>> # A tuple can also be used to specify a discrete set
234236
>>> sampler = (1, 3, 5, 7, 9)
235237
>>> # Select randomly between two matrices

mitxgraders/helpers/calc/expressions.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def get_grammar(self):
389389
plus = Literal("+")
390390

391391
# Also accept unicode emdash
392-
emdash = Literal("\u2014")
392+
emdash = Literal("\u2014")
393393
emdash.setParseAction(lambda: "-")
394394

395395
minus = Literal("-") | emdash
@@ -776,12 +776,18 @@ def eval_variable(parse_result, variables):
776776
Returns a copy of the variable in self.vars
777777
778778
We return a copy so that nothing in self.vars is mutated.
779+
780+
If the variable is a long integer, we convert it to a float so that numpy methods work on it.
779781
780782
NOTE: The variable's value's class must implement a __copy__ method.
781783
(numpy ndarrays do implement this method)
782784
"""
783785
value = variables[parse_result[0]]
784-
return copy.copy(value)
786+
value = copy.copy(value)
787+
# Convert python long integers to floats
788+
if isinstance(value, six.integer_types):
789+
value = float(value)
790+
return value
785791

786792
@staticmethod
787793
def eval_function(parse_result, functions):

mitxgraders/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"""
55
from __future__ import print_function, division, absolute_import, unicode_literals
66

7-
__version__ = "2.3.1"
7+
__version__ = "2.3.2"

python_lib.zip

77 Bytes
Binary file not shown.

tests/helpers/calc/test_expressions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,6 @@ def test_min_max():
275275
r"Expected at least 2 inputs, but received 1.")
276276
with raises(ArgumentError, match=msg):
277277
evaluator('max(1.5)')
278+
279+
def test_large_numbers():
280+
assert evaluator('x', variables={'x': 10 ** 30})[0] == 1e30

0 commit comments

Comments
 (0)