formelsammlung.strcalc module

formelsammlung.strcalc.calculate_string(expression)[source]

Calculate the given expression.

The given arithmetic expression string is parsed as an ast and then handled by the ast.NodeVisitor.

Python exceptions are risen like with normal arithmetic expression e.g. ZeroDivisionError.

Supported number types:

Warning

On PyPy3 only: When working with complex numbers containing or resulting with float numbers be aware that the result of calculate_string() and the equivalent arithmetic expression can divert in the decimals. The result from calculate_string() is then less precise.

Supported mathematical operators:

How to use:

>>> calculate_string("(1+2)/3")
1.0
Parameters

expression (str) – String with arithmetic expression.

Return type

Union[int, float, complex, None]

Returns

Result or None