MPe
MPe

Reputation: 430

Sympy - Comparing two string inputs causes unexpected result

I've got a new problem with parsing two input-expressions:

I would like to compare the two input experessions (strings). What I've done jet:

arg1=parse_expr(sys.argv[1])                   #Here it has to/can use the complete simplified expression
arg2=parse_expr(sys.argv[2], evaluate=False)   #Here it has to use the unsimplified expression

if(arg1==arg2 and arg1-arg2==0):
     print "correct"

if(arg1!=arg2 and arg1-arg2==0):
     print "go on calculating"

if(arg1!=arg2 and arg1-arg2!=0):
     print "wrong"

When I enter only two fractions, I get not the result that I would actually expect. e.g.: 2/3==2/3 => False But 2/3-2/3 ==> True

I also tried to use "sympify" and "kernS" instead of "parse_expr" with nearly the same result.

Here I'm going to enter 2/3 and 4/6 which is obviously the same fraction but not identical. I would like to compare the two strings in their original expression. Here I should get the return value "go on calculating" but I'll get "correct". Thats because of sympy simplyfies the second expression and compares 2/3==2/3

Can anybody help me? Thanks a lot!

Upvotes: 0

Views: 111

Answers (1)

smichr
smichr

Reputation: 19029

I would suggest that you use

if (arg1.as_numer_denom() == arg2.as_numer_denom() and 
    simplify(arg1 - arg2) == 0):

and similar in the other tests.

Upvotes: 3

Related Questions