Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sympify TypeError "Invalid comparison of complex zoo" with '1<zoo' #18077

Open
ducktalez opened this issue Dec 19, 2019 · 1 comment
Open

sympify TypeError "Invalid comparison of complex zoo" with '1<zoo' #18077

ducktalez opened this issue Dec 19, 2019 · 1 comment
Labels

Comments

@ducktalez
Copy link

I reduced my problem to this expression:
sympy.sympify('1<zoo')
Which will raise a TypeError:
TypeError: Invalid comparison of complex zoo

zoo occurs during an 'accidental' division by zero that seldomly occurs. The original comparisson also has nothing to do with complex numbers: 1<1/(a-a). And, of course, this expression does not make sense.

However, I expected sympy to evaluate to 'nan' and not to crash.
Is this intentionally?
Is there a way to return 'nan' without a try-block? Or specify fail-evaluations as proposed somewhere in #5195 so I could choose a (signed) oo instead of zoo to have a secure evaluation?

.

Complete Exception:

>>> import sympy
>>> sympy.sympify('1<zoo')
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-112-fa2832916d99>", line 1, in <module>
    sympy.sympify('1<zoo')
  File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\core\sympify.py", line 368, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\parsing\sympy_parser.py", line 965, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\parsing\sympy_parser.py", line 878, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "<string>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\core\numbers.py", line 2185, in __lt__
    return Rational.__lt__(self, other)
  File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\core\numbers.py", line 1818, in __lt__
    return Expr.__lt__(expr, other)
  File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\core\expr.py", line 334, in __lt__
    raise TypeError("Invalid comparison of complex %s" % me)
TypeError: Invalid comparison of complex zoo

@oscarbenjamin
Copy link
Contributor

There is no way to know whether 1/0 is positive or negative so evaluating to oo rather than -oo is not correct.

I think there is an issue somewhere that proposes that something like 1 < zoo could just be False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants