From ebbbbb3e5b3a8fe1991a7f869de6f7bab60f8641 Mon Sep 17 00:00:00 2001 From: Li-Xiang-Ideal <54926635+Li-Xiang-Ideal@users.noreply.github.com> Date: Sun, 17 Dec 2023 20:57:57 +0800 Subject: [PATCH] Fix Range for negative di --- mathics/builtin/list/constructing.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mathics/builtin/list/constructing.py b/mathics/builtin/list/constructing.py index f1ee2ef7e..4b43557f4 100644 --- a/mathics/builtin/list/constructing.py +++ b/mathics/builtin/list/constructing.py @@ -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.} @@ -258,7 +261,8 @@ 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 ) @@ -266,9 +270,13 @@ def eval(self, imin, imax, di, evaluation: Evaluation): 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