There is an `nsimplify` function that you can link up to a function that will change floats to Rationals:
def nrat(e, maxden=10): f = e.atoms(Float) reps = {} for i in f: r = nsimplify(i, rational=True) if r.q <= maxden: reps[i] = r return e.xreplace(reps) >>> nrat(1/2 + 13/17*x) 0.764705882352941*x + 1/2 >>> nrat(1/2 + 13/17*x, 20) 13*x/17 + 1/2 >>> nrat(x**(1/2)) sqrt(x) /c On Saturday, May 15, 2021 at 11:43:13 PM UTC-5 asme...@gmail.com wrote: > On Sat, May 15, 2021 at 3:02 PM David Bailey <da...@dbailey.co.uk> wrote: > > > > On 14/05/2021 23:01, Oscar Benjamin wrote: > > > > That's a great video but it's describing something very different. There > he types exact input into the calculator and then calculator somehow makes > a numeric/symbolic approximation that goes wrong. I'm not talking about > changing anything for exact input. I'm only referring to the first step of > passing a float to sympy. Maybe we need a more limited version of nsimplify > for this. I can certainly see the reasons for not trying to convert float > to Rational but I can also see how much of a problem it is for users. The > "power users" will not have so much of a problem converting their input to > Float if that's what they meant. > > > > > > Yes, but surely that would just lead to a new set of anomalies because > the float has finite precision. I mean it would be possible to translate > floats back into fractions for simple fractions like 3/4, but what about > 123456789123456788/123456789123456789? Also, people sometimes pass > intentional floats to SymPy without being power users! > > > > Wouldn't it be possible to persuade the Python team to implement > auto_int_to_Integer outside of IPython - it can't be hard to do? > > My argument is that it's really only a good idea to do this in an > interactive environment. In that case, you are probably using > something like IPython or Jupyter notebook. > > It would in theory be possible to make it work in a Python script > using a codec trick > (https://rahul.gopinath.org/post/2019/12/25/python-macros/). At that > point, you are more or less creating your own DSL. > > For any kind of upstream change to Python itself, you have to remember > that 99% of Python users don't want this feature. So you'd have to > implement it in a way that doesn't affect them. > > Aaron Meurer > > > > > David > > > > > > > > > > -- > > 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 view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/c7fa2c5f-b7c3-4e2d-bbfd-29b8ad81e553%40dbailey.co.uk > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/d87b17f3-85a5-4a89-8e6e-7464d368cc0cn%40googlegroups.com.