Hi, On 20 October 2014 19:18, Marek Kaluba <[email protected]> wrote: > Hi all, > > recently I started using sympy (and immediately fall for it), so this mail > may be simply my lack of understanding how sympy internally works. > However I found a _strange_ (to me) issue with numerical evaluation: > > (ipython console) > from sympy import * > N(1/3) > 0 //integer division, ok > N(Rational(1)/Rational(3),20) > 0.33333333333333333333 //numerical approximation of 1/3, ok > > init_session() //for nice latex printing and default variables -- I guess > this is what init_session() does?? > [...] //standard output > > pprint(N(1/3)) > 0.333333333333333 // why not 0?
This is because init_session() issues `from __future__ import division`, so now 1/3 is equivalent to float(1)/3. > pprint(N(1/3,100)) > 0.3333333333333333148296162562473909929394721984863281250000000000000000000000 > 000000000000000000000000 > // what is happening here?? As 1/3 is a float in this case, thus SymPy can't compute it's numerical value to the expected precision. You can use init_session(auto_int_to_Integer=True) get the desired behavior, where 1/3 is understood as Integer(1)/3. In general, handling rationals in SymPy can be tricky, so if unsure, use type(1/3) to get feedback what you are dealing with. Mateusz > I'd be grateful if anyone can explain this to me... > > Cheers, > Mark > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/c3a412a0-7e30-4b34-8f9d-b521991bd40e%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAGBZUCbWZtwKmeQ3QzGjbK8ki3ozSUGROWWadGG0mFZ4VGFcRA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
