omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Sympy solve() rounds to 0?

    Pythonista
    3
    4
    2592
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • donnieh
      donnieh last edited by donnieh

      I am passing solve() big and small numbers such as electron_charge = 1.6e-19

      The function rounds those small numbers to 0. Has anyone seen this? An example is below.

      from sympy import *
      x, y, z = symbols('x y z')
      
      # this works because its only 1e-5
      rslt = solve(Eq(0+1e-5, x), x)
      rslt = rslt[0]
      print rslt
      
      #this outputs zero cause number is too small? :(
      rslt = solve(Eq(0+1e-10, x), x)
      rslt = rslt[0]
      print rslt
      
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        you will want the rational=False flag for solve. otherwise it tries to find approximate rational expressions.

        1 Reply Last reply Reply Quote 1
        • dgelessus
          dgelessus last edited by dgelessus

          Try adding the flag rational=False to the solve call:

          solve(Eq(1e-10, x), x, rational=False)
          

          By default, sympy converts floats to rationals and apparently this causes precision issues (even though when I do Float(Rational(1e-10)) by hand I see no precision loss, but whatever).

          @omz According to this discussion on Stack Overflow something related to this has been fixed in sympy 0.7.5, and Pythonista comes with 0.7.4.1. Might be worth updating if you haven't done so already internally.

          1 Reply Last reply Reply Quote 2
          • donnieh
            donnieh last edited by

            Oh ok! Thanks guys! Very helpful.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors