Skip to content

Commit 3479ad6

Browse files
committed
fix comparison for reals and other types
1 parent 67a07ea commit 3479ad6

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

mathics/builtin/comparison.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from mathics.builtin.base import BinaryOperator, Builtin, SympyFunction
1010
from mathics.core.expression import (Complex, Expression, Integer, Number,
11-
String, Symbol)
11+
Real, String, Symbol)
1212
from mathics.core.numbers import dps
1313

1414

@@ -324,25 +324,28 @@ def do_cmp(x1, x2):
324324
):
325325
return None
326326

327-
# rely on sympy for better comparisons
328327
s1 = x1.to_sympy()
329328
s2 = x2.to_sympy()
330329

331-
# this might seem like poorly written code at first glance, but
332-
# the equality comparison is required for it to function properly.
333-
# Sympy comparisons might not return sometimes, and will raise
334-
# an error in that case if not compared to True/False.
330+
# use internal comparisons only for Reals
331+
# and use sympy for everything else
332+
if s1.is_Float and s2.is_Float:
333+
if x1 == x2:
334+
return 0
335+
if x1 < x2:
336+
return -1
337+
return 1
335338

336-
eq = sympy.Equality(s1, s2)
339+
# we don't want to compare anything that
340+
# cannot be represented as a numeric value
341+
if s1.is_number and s2.is_number:
342+
if s1 == s2:
343+
return 0
344+
if s1 < s2:
345+
return -1
346+
return 1
337347

338-
if eq == True:
339-
return 0
340-
elif eq != False:
341-
return None
342-
343-
if sympy.StrictLessThan(s1, s2) == True:
344-
return -1
345-
return 1
348+
return None
346349

347350

348351
class SympyComparison(SympyFunction):
@@ -766,7 +769,7 @@ class Min(_MinMax):
766769
<dd>returns the expression with the lowest value among the $e_i$.
767770
</dl>
768771
769-
Minimum of a series of numbers:
772+
Minimum of a series of values:
770773
>> Min[4, -8, 1]
771774
= -8
772775
>> Min[E - Pi, Pi, E + Pi, 2 E]

0 commit comments

Comments
 (0)