stefnotch / quantum-sheet

QuantumSheet - A user friendly mathematics worksheet and solver
https://stefnotch.github.io/quantum-sheet/
GNU General Public License v3.0
55 stars 5 forks source link

Floats not always desired? #28

Open phcreery opened 2 years ago

phcreery commented 2 years ago

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
>>>
phcreery commented 2 years ago

Here is a little fix I came up with. Line 133 of https://github.com/phcreery/quantum-sheet/commit/0984fe9ca630cbb71f29771f49c9bc6fcaad0644 I don't see this hurting anything. Let me know what you think.

stefnotch commented 2 years ago

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.