formelsammlung.strcalc module¶
-
formelsammlung.strcalc.calculate_string(expression)[source]¶ Calculate the given expression.
The given arithmetic expression string is parsed as an
astand then handled by theast.NodeVisitor.Python exceptions are risen like with normal arithmetic expression e.g.
ZeroDivisionError.Supported number types:
Warning
On PyPy3 only: When working with
complexnumbers containing or resulting withfloatnumbers be aware that the result ofcalculate_string()and the equivalent arithmetic expression can divert in the decimals. The result fromcalculate_string()is then less precise.Supported mathematical operators:
Positive (
operator.pos())+ aNegative (
operator.neg())- aAddition (
operator.add())a + bSubtraction (
operator.sub())a - bMultiplication (
operator.mul())a * bExponentiation (
operator.pow())a ** bDivision (
operator.truediv())a / bFloorDivision (
operator.floordiv())a // bModulo (
operator.mod())a % b
How to use:
>>> calculate_string("(1+2)/3") 1.0