Skip to content

Fix RoundOff problems

Markus Holzer requested to merge holzer/pystencils:RoundOffError into master

When printing Rationals with pystencils at the moment the number of digits which will be printed is only 15. This is not enough for DP and thus Error is introduced in the printed code...

The reason for this is the following line: str(expr.evalf().num)

Here it is important to know what is happening. First of all the Rational is evaluated with Sympy with 15 digits, because SymPy defaults to 15 digits when using evalf: https://docs.sympy.org/latest/modules/evalf.html

but the story continues: .num is not a SymPy function !!!! This comes from mpmath so the Rational is converted to an sp.Float and then to a mpmath.ctx_mp_python.mpf. And yet again mpmath provides also a notion of precision. This could be set with mpmath.mp.dps = 30 for example. However, why should this be done in that way here anyway?

In this MR the .num function is removed and all evalf functions in pystencils are set to evalf(17). Note here that the SymPy printer on the other hand knows very well which precision it needs to provide. Thus when using a float inside the Assignment SymPy prints this correctly.

Merge request reports