Hi,
The evalf function is intended to compute the result to any given
specified precision and return a result such that it is correct to
almost every digit. However evalf needs access to the full expression
to be able to do that. If you first substitute a Float in because the
expression will partially or fully evaluate before evalf gets a
chance:
In [5]: expr = cos(2*x)
In [6]: expr.evalf(50, subs={x:2.4})
Out[6]: 0.087498983439446392365833650247410931894111629665389
In [7]: expr.subs(x, 2.4).evalf(50)
Out[7]: 0.087498983439446398335803678492084145545959472656250
Note that eval isn't doing anything in the second example because the
subs already evaluated to a Float (ignoring our request for 50
digits):
In [10]: expr.subs(x, 2.4)
Out[10]: 0.0874989834394464
The first example gives an accurate result for cos(2x) based on the
true value of the float 2.4 (which is not exactly equal to 12/5). If
we really want to calculate cos(2x) for x=12/5 with minimal rounding
error we should use:
In [9]: expr.subs(x, Rational('2.4')).evalf(50)
Out[9]: 0.087498983439446569320215257649487633957449890596100
--
Oscar
> --
> 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/03831493-431b-45e7-809f-6cc691cab191n%40googlegroups.com.