You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Floating point "add" is non-associative, i.e. (x+y)+z often differs from x+(y+z). In SymPy Add (re)orders its arguments (for good reasons), but for representing e.g. an algorithm in finite precision this creates a problem (see e.g. Kahan summation on wikipedia).
Typically we use UnevaluatedExpr for situations like this:
I propose that we fix this in the printers (I suspect updating precedence for UnevaluatedExpr might be enough, I haven't tried yet though). At the very least, the code printers should output parenthesis, but I think it's reasonable for the StrPrinter to do so as well?
The text was updated successfully, but these errors were encountered:
importsympyfromIPython.displayimportdisplayfromsympy.abcimportx, y, zexpr=x-sympy.UnevaluatedExpr(y-z)
print(expr) # prints: x - (y - z)display(expr) # prints: x - y - zprint(sympy.__version__) # 1.8
Floating point "add" is non-associative, i.e.
(x+y)+z
often differs fromx+(y+z)
. In SymPyAdd
(re)orders its arguments (for good reasons), but for representing e.g. an algorithm in finite precision this creates a problem (see e.g. Kahan summation on wikipedia).Typically we use
UnevaluatedExpr
for situations like this:but, as you see the printer is not emitting the required parenthesis to make this useful for representing non-associative add.
Note that SymEngine gets this right:
I propose that we fix this in the printers (I suspect updating precedence for
UnevaluatedExpr
might be enough, I haven't tried yet though). At the very least, the code printers should output parenthesis, but I think it's reasonable for theStrPrinter
to do so as well?The text was updated successfully, but these errors were encountered: