You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ecd832b
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/postfix.rst
+248-2Lines changed: 248 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,255 @@ Postfix
2
2
=======
3
3
4
4
The postfix notation, also known as `Reverse Polish Notation (RPN)`_, is a mathematical notation in which every operator follows all of its operands.
5
-
It is used in mathparse to evaluate expressions efficiently.
5
+
It is used in mathparse to evaluate expressions efficiently and safely.
6
6
7
-
Additionally, mathparse does not employ the use of Python's :py:func:`eval` function when evaluating provided mathematical expressions.
7
+
Notably, mathparse does not employ the use of Python's :py:func:`eval` function when evaluating provided mathematical expressions.
8
8
This is a measure to prevent arbitrary code execution vulnerabilities.
9
9
10
+
Postfix Notation
11
+
----------------
12
+
13
+
In postfix notation, mathematical operators are placed after their operands, eliminating the need for parentheses to specify the order of operations. This permits evaluation using a stack-based algorithm.
14
+
15
+
Basic Examples
16
+
~~~~~~~~~~~~~~
17
+
18
+
Some examples comparing infix and postfix notation are as follows:
19
+
20
+
.. list-table:: Infix vs Postfix Examples
21
+
:widths: 40 40 20
22
+
:header-rows: 1
23
+
24
+
* - Infix Expression
25
+
- Postfix Expression
26
+
- Result
27
+
* - ``3 + 4``
28
+
- ``3 4 +``
29
+
- ``7``
30
+
* - ``5 * (2 + 3)``
31
+
- ``5 2 3 + *``
32
+
- ``25``
33
+
* - ``(8 - 3) * 2``
34
+
- ``8 3 - 2 *``
35
+
- ``10``
36
+
* - ``6 / 2 + 1``
37
+
- ``6 2 / 1 +``
38
+
- ``4``
39
+
40
+
Stack-Based Evaluation
41
+
~~~~~~~~~~~~~~~~~~~~~~
42
+
43
+
Postfix expressions are evaluated using a stack:
44
+
45
+
1. **Scan from left to right**: Process each token in the expression
46
+
2. **Numbers**: Push numbers onto the stack
47
+
3. **Operators**: Pop required operands from stack, apply operation, push result back
48
+
4. **Final result**: The remaining value on the stack is the answer
49
+
50
+
Example evaluation of ``3 4 + 2 *``:
51
+
52
+
.. code-block:: text
53
+
54
+
Token: 3 Stack: [3]
55
+
Token: 4 Stack: [3, 4]
56
+
Token: + Stack: [7] (pop 4, pop 3, push 3+4=7)
57
+
Token: 2 Stack: [7, 2]
58
+
Token: * Stack: [14] (pop 2, pop 7, push 7*2=14)
59
+
Result: 14
60
+
61
+
Postfix vs Infix Notation
62
+
--------------------------
63
+
64
+
Understanding the differences between postfix and infix notation helps explain why mathparse uses the postfix approach.
65
+
66
+
Infix Notation (Traditional)
67
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68
+
69
+
**Advantages:**
70
+
- Natural and intuitive for humans
71
+
- Matches mathematical conventions
72
+
- Easy to read and write
73
+
74
+
**Disadvantages:**
75
+
- Requires parentheses for complex expressions
76
+
- Needs operator precedence rules (PEMDAS/BODMAS)
77
+
- More complex to parse and evaluate programmatically
78
+
- Ambiguity without proper precedence handling
79
+
80
+
**Example challenges:**
81
+
- ``2 + 3 * 4`` could be interpreted as ``(2 + 3) * 4 = 20`` or ``2 + (3 * 4) = 14``
82
+
- ``a - b + c`` requires left-to-right evaluation rules
83
+
84
+
Postfix Notation (RPN)
85
+
~~~~~~~~~~~~~~~~~~~~~~
86
+
87
+
**Advantages:**
88
+
- No parentheses needed - order is explicit
89
+
- No operator precedence rules required
90
+
- Simple, efficient evaluation algorithm
91
+
- Unambiguous expression evaluation
92
+
- Natural fit for stack-based computation
93
+
94
+
**Disadvantages:**
95
+
- Less intuitive for humans initially
96
+
- Requires learning a different notation system
97
+
98
+
**Why mathparse uses postfix:**
99
+
100
+
1. **Simplicity**: Stack-based evaluation is straightforward and reliable
101
+
2. **Efficiency**: Linear time complexity with minimal memory overhead
102
+
3. **Safety**: No need for complex parsing that could introduce vulnerabilities
103
+
4. **Correctness**: Eliminates ambiguity in expression evaluation
104
+
105
+
Implementation in mathparse
106
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
107
+
108
+
The mathparse library converts infix expressions to postfix using the `Shunting-yard algorithm`_:
By using postfix notation and avoiding :py:func:`eval`, mathparse provides a secure, efficient, and reliable way to evaluate mathematical expressions from untrusted sources.
0 commit comments