Floats not always desired?
phcreery opened this issue · 2 comments
Example:
>>> _HI = sympy.symbols('a')
>>> sympy.expand(sympy.Pow(sympy.Add(_HI,sympy.Float(1)),sympy.Float(2)))
(a + 1.0)**2.0
>>> sympy.expand(sympy.Pow(sympy.Add(_HI,sympy.Float(1)),sympy.Integer(2)))
a**2 + 2.0*a + 1.0
>>>
Here is a little fix I came up with. Line 133 of phcreery@0984fe9 I don't see this hurting anything. Let me know what you think.
Sorry for the late response. I took a look at this and yes, it does seem like the current default behavior is not ideal.
https://docs.sympy.org/latest/gotchas.html#evaluating-expressions-with-floats-and-rationals
Instead, we should almost always use the Integer
and Rational
data types. I think the conversions in Sympy should always work out the right way.
One minor gotcha of that approach is that the Integer
and Rational
data types can become arbitrarily large. That means if someone types 99999999**9999999
, Sympy will painstakingly try to compute the entire result, regardless of how long it takes. To "fix" that, we have to interrupt the execution after a few seconds.