Skip to content

Commit

Permalink
Merge pull request Mathics3#951 from Mathics3/fix-range
Browse files Browse the repository at this point in the history
Fix ``Range`` for negative ``di``
  • Loading branch information
rocky authored Dec 17, 2023
2 parents ebf604d + ebbbbb3 commit 4d52275
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions mathics/builtin/list/constructing.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ class Range(Builtin):
>> Range[-3, 2]
= {-3, -2, -1, 0, 1, 2}
>> Range[5, 1, -2]
= {5, 3, 1}
>> Range[1.0, 2.3]
= {1., 2.}
Expand Down Expand Up @@ -258,17 +261,22 @@ def eval(self, imin, imax, di, evaluation: Evaluation):
and isinstance(imax, Integer)
and isinstance(di, Integer)
):
result = [Integer(i) for i in range(imin.value, imax.value + 1, di.value)]
pm = 1 if di.value >= 0 else -1
result = [Integer(i) for i in range(imin.value, imax.value + pm, di.value)]
return ListExpression(
*result, elements_properties=range_list_elements_properties
)

imin = imin.to_sympy()
imax = imax.to_sympy()
di = di.to_sympy()

def compare_type(a, b):
return a <= b if di >= 0 else a >= b

index = imin
result = []
while index <= imax:
while compare_type(index, imax):
evaluation.check_stopped()
result.append(from_sympy(index))
index += di
Expand Down

0 comments on commit 4d52275

Please sign in to comment.