Comparison failure

17 views
Skip to first unread message

Alexander Lindsay

unread,
May 23, 2016, 3:58:35 PM5/23/16
to sympy
Hi all,

I am working on a pull request. When writing tests for the PR, I keep encountering false comparisons when the expressions I'm comparing appear to be the exact same. Example shown below:

In[80]: %paste
    result
= manualintegrate(x**(3/2) * log(x), x)
    desired_result
= 0.4*x**2.5*log(x) - 0.16*x**2.5
   
print(result)
   
print(desired_result)
   
print(result == desired_result)
   
assert result == desired_result
## -- End pasted text --
0.4*x**2.5*log(x) - 0.16*x**2.5
0.4*x**2.5*log(x) - 0.16*x**2.5
False
Traceback (most recent call last):
 
File "/home/lindsayad/miniconda2/envs/python3SympyDev/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
   
exec(code_obj, self.user_global_ns, self.user_ns)
 
File "<ipython-input-80-5bc67a132f24>", line 6, in <module>
   
assert result == desired_result
AssertionError

Any suggestions on how to make these comparisons work?

Aaron Meurer

unread,
May 23, 2016, 4:01:06 PM5/23/16
to sy...@googlegroups.com
My guess is that it's an issue with floating point accuracy. Unless
you are testing floating point numbers directly, I would suggest
making all values in the expressions rational. To accurately test
floating point numbers, you generally have to check that the
difference is below some small epsilon (like 1e-15), which can be
annoying if they appear throughout an expression.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/942dfeea-3dc8-4655-817a-ecdaa0589299%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages