0

I am running a code that takes different inputs (generated by another code) as strings and I need to turn them into sympy expressions. I use from sympy.parsing.sympy_parser import parse_expr and it works well, however sometimes I get really big numbers (which are wrong expressions), but i can't check beforehand, as they come as strings and when I run parse_expr on them it takes forever. One such example is below:

from sympy.parsing.sympy_parser import parse_expr
eq = "tan(sin(7.10414690881377e-30370322717341*x0) - 9183346819072.0)"
parse_expr(eq)

Again, in the actual code I don't know eq before hand. Is there an easy way to avoid this. For example to set a time limit on parse_expr? Or to set a limit on the maximum value allowed for a parameter in eq?

JohnDoe122
  • 638
  • 9
  • 23
  • 2
    This is very slow for me on master and with sympy 1.6. It's trying to compute `Rational(710414690881377, 10**30370322717355)`. This is a bug that should be reported https://github.com/sympy/sympy/issues – Oscar Benjamin Jun 20 '20 at 11:30
  • No idea why, but I'm also running SymPy 1.6 and can run the code snippet in ~0.06 seconds. However, t just halts on @OscarBenjamin's one. To set timeouts on a single threaded application, see [this](https://stackoverflow.com/questions/492519/timeout-on-a-function-call). You could also use regex to check if there are more than, say, 5 digits in a row before a `.` or after an `e`. – Chris du Plessis Jun 21 '20 at 15:47
  • I even get stuck on `parse_expr("7.14e-30370322")`, however `parse_expr("7.14e-3037032")` seems reasonably fast. Also sympy 1.6 – laolux Jun 22 '20 at 13:08

0 Answers0