Skip to content

Commit 6f1b9be

Browse files
committed
WIP - Better comparisons between ExactNumbers
1 parent b8343a4 commit 6f1b9be

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

mathics/builtin/comparison.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,12 @@ def do_compare(self, l1, l2) -> Union[bool, None]:
254254
def apply(self, items, evaluation):
255255
"%(name)s[items___]"
256256
items_sequence = items.get_sequence()
257-
if len(items_sequence) <= 1:
257+
n = len(items_sequence)
258+
if n <= 1:
258259
return SymbolTrue
260+
is_exact_vals = [Expression("ExactNumberQ", arg).evaluate(evaluation) for arg in items_sequence]
261+
if all(val == SymbolTrue for val in is_exact_vals):
262+
return self.apply_other(items, evaluation)
259263
args = self.numerify_args(items, evaluation)
260264
wanted = operators[self.get_name()]
261265
for x, y in itertools.combinations(args, 2):
@@ -274,7 +278,7 @@ def apply(self, items, evaluation):
274278
return SymbolTrue
275279

276280
def apply_other(self, args, evaluation):
277-
"%(name)s[args___?(!RealNumberQ[#]&)]"
281+
"%(name)s[args___?(!ExactNumberQ[#]&)]"
278282
args = args.get_sequence()
279283
for x, y in itertools.combinations(args, 2):
280284
c = self.do_compare(x, y)
@@ -285,6 +289,7 @@ def apply_other(self, args, evaluation):
285289
return SymbolTrue
286290

287291

292+
288293
class _ComparisonOperator(_InequalityOperator):
289294
"Compares arguments in a chain e.g. a < b < c compares a < b and b < c."
290295

mathics/core/expression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ def __new__(cls, value) -> 'MachineReal':
22182218
def to_python(self, *args, **kwargs) -> float:
22192219
return self.value
22202220

2221-
def to_sympy(self):
2221+
def to_sympy(self, *args, **kwargs):
22222222
return sympy.Float(self.value)
22232223

22242224
def to_mpmath(self):

test/test_compare.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
from .helper import check_evaluation
3+
4+
def test_compare():
5+
for str_expr, str_expected in (
6+
(
7+
"I == I",
8+
"True",
9+
),
10+
(
11+
"I == 0",
12+
"False",
13+
),
14+
(
15+
"I + 0 == 1 I - 0",
16+
"True",
17+
),
18+
(
19+
"I + 5 == I",
20+
"False",
21+
),
22+
):
23+
check_evaluation(str_expr, str_expected)

0 commit comments

Comments
 (0)