Sympy 简明教程

SymPy - evalf() function

此函数会计算给定的数值表达式,其浮点精度最高为 100 位。此函数还使用 subs 参数获取符号的数值字典对象。考虑以下表达式

This function evaluates a given numerical expression upto a given floating point precision upto 100 digits. The function also takes subs parameter a dictionary object of numerical values for symbols. Consider following expression

>>> from sympy.abc import r
>>> expr=pi*r**2
>>> expr

上面的代码片段给出的输出等同于以下表达式 −

The above code snippet gives an output equivalent to the below expression −

$\Pi{r^2}$

$\Pi{r^2}$

要使用 evalf() 函数求值以上表达式,请用 5 替换 r

To evaluate above expression using evalf() function by substituting r with 5

>>> expr.evalf(subs={r:5})

上面的代码段给出了以下输出:

The above code snippet gives the following output −

78.5398163397448

78.5398163397448

默认情况下,浮点精度最高为 15 位,可由 100 以内的任何数字覆盖。以下表达式的精度求值最高为 20 位。

By default, floating point precision is upto 15 digits which can be overridden by any number upto 100. Following expression is evaluated upto 20 digits of precision.

>>> expr=a/b
>>> expr.evalf(20, subs={a:100, b:3})

上面的代码段给出了以下输出:

The above code snippet gives the following output −

33.333333333333333333

33.333333333333333333