Skip to content

Commit 04ebdb3

Browse files
committed
* a fraction is zero if the numerator is zero and the denominator is not zero. \n* fix MachineReal.is_approx_zero
1 parent 12ff672 commit 04ebdb3

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

mathics/builtin/arithmetic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,11 @@ def apply(self, expr, evaluation):
13461346

13471347
sympy_expr = expr.to_sympy()
13481348
result = _iszero(sympy_expr)
1349+
if result is None:
1350+
# try expanding the expression
1351+
exprexp = Expression("ExpandAll", expr).evaluate(evaluation)
1352+
exprexp = exprexp.to_sympy()
1353+
result = _iszero(exprexp)
13491354
if result is None:
13501355
# Can't get exact answer, so try approximate equal
13511356
numeric_val = Expression("N", expr).evaluate(evaluation)

mathics/core/evaluation.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ def __init__(
264264
def parse(self, query):
265265
"Parse a single expression and print the messages."
266266
from mathics.core.parser import SingleLineFeeder
267-
268267
return self.parse_feeder(SingleLineFeeder(query))
269268

270269
def parse_evaluate(self, query, timeout=None):
@@ -278,7 +277,6 @@ def parse_feeder(self, feeder):
278277
def parse_feeder_returning_code(self, feeder):
279278
"Parse a single expression from feeder and print the messages."
280279
from mathics.core.parser.util import parse_returning_code
281-
282280
try:
283281
result, source_code = parse_returning_code(self.definitions, feeder)
284282
except TranslateError:

mathics/core/expression.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ def __neg__(self) -> 'Rational':
21092109

21102110
@property
21112111
def is_zero(self) -> bool:
2112-
return self.numerator().is_zero
2112+
return self.numerator().is_zero and not self.denominator().is_zero()
21132113

21142114

21152115
class Real(Number):
@@ -2257,9 +2257,10 @@ def is_zero(self) -> bool:
22572257

22582258
@property
22592259
def is_approx_zero(self) -> bool:
2260-
# FIXME: figure out how to hook int $MachinePrecision and
2261-
# what the right definition here would be.
2262-
return abs(self.value) <= 10**-14
2260+
# In WMA, Chop[10.^(-10)] == 0,
2261+
# so, lets take it.
2262+
res = abs(self.value) <= 1e-10
2263+
return res
22632264

22642265

22652266
class PrecisionReal(Real):

0 commit comments

Comments
 (0)