1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>rpntutorial</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:root@localhost" />
8 </head>
10 <body style="background-color: white">
12 <p><a name="__index__"></a></p>
13 <!-- INDEX BEGIN -->
14 <!--
16 <ul>
18 <li><a href="#name">NAME</a></li>
19 <li><a href="#description">DESCRIPTION</a></li>
20 <li><a href="#reading_comparison_operators">Reading Comparison Operators</a></li>
21 <li><a href="#reading_the_if_operator">Reading the IF Operator</a></li>
22 <li><a href="#some_examples">Some Examples</a></li>
23 <li><a href="#exercises">Exercises</a></li>
24 <li><a href="#author">AUTHOR</a></li>
25 </ul>
26 -->
27 <!-- INDEX END -->
29 <p>
30 </p>
31 <h1><a name="name">NAME</a></h1>
32 <p>rpntutorial - Reading RRDtool RPN Expressions by Steve Rader</p>
33 <p>
34 </p>
35 <hr />
36 <h1><a name="description">DESCRIPTION</a></h1>
37 <p>This tutorial should help you get to grips with RRDtool RPN expressions
38 as seen in CDEF arguments of RRDtool graph.</p>
39 <p>
40 </p>
41 <hr />
42 <h1><a name="reading_comparison_operators">Reading Comparison Operators</a></h1>
43 <p>The LT, LE, GT, GE and EQ RPN logic operators are not as tricky as
44 they appear. These operators act on the two values on the stack
45 preceding them (to the left). Read these two values on the stack
46 from left to right inserting the operator in the middle. If the
47 resulting statement is true, then replace the three values from the
48 stack with ``1''. If the statement if false, replace the three values
49 with ``0''.</p>
50 <p>For example, think about ``2,1,GT''. This RPN expression could be
51 read as ``is two greater than one?'' The answer to that question is
52 ``true''. So the three values should be replaced with ``1''. Thus the
53 RPN expression 2,1,GT evaluates to 1.</p>
54 <p>Now consider ``2,1,LE''. This RPN expression could be read as ``is
55 two less than or equal to one?''. The natural response is ``no''
56 and thus the RPN expression 2,1,LE evaluates to 0.</p>
57 <p>
58 </p>
59 <hr />
60 <h1><a name="reading_the_if_operator">Reading the IF Operator</a></h1>
61 <p>The IF RPN logic operator can be straightforward also. The key
62 to reading IF operators is to understand that the condition part
63 of the traditional ``if X than Y else Z'' notation has *already*
64 been evaluated. So the IF operator acts on only one value on the
65 stack: the third value to the left of the IF value. The second
66 value to the left of the IF corresponds to the true (``Y'') branch.
67 And the first value to the left of the IF corresponds to the false
68 (``Z'') branch. Read the RPN expression ``X,Y,Z,IF'' from left to
69 right like so: ``if X then Y else Z''.</p>
70 <p>For example, consider ``1,10,100,IF''. It looks bizarre to me.
71 But when I read ``if 1 then 10 else 100'' it's crystal clear: 1 is true
72 so the answer is 10. Note that only zero is false; all other values
73 are true. ``2,20,200,IF'' (``if 2 then 20 else 200'') evaluates to 20.
74 And ``0,1,2,IF'' (``if 0 then 1 else 2) evaluates to 2.</p>
75 <p>Notice that none of the above examples really simulate the whole
76 ``if X then Y else Z'' statement. This is because computer programmers
77 read this statement as ``if Some Condition then Y else Z''. So it's
78 important to be able to read IF operators along with the LT, LE,
79 GT, GE and EQ operators.</p>
80 <p>
81 </p>
82 <hr />
83 <h1><a name="some_examples">Some Examples</a></h1>
84 <p>While compound expressions can look overly complex, they can be
85 considered elegantly simple. To quickly comprehend RPN expressions,
86 you must know the algorithm for evaluating RPN expressions:
87 iterate searches from the left to the right looking for an operator.
88 When it's found, apply that operator by popping the operator and some
89 number of values (and by definition, not operators) off the stack.</p>
90 <p>For example, the stack ``1,2,3,+,+'' gets ``2,3,+'' evaluated (as ``2+3'')
91 during the first iteration and is replaced by 5. This results in
92 the stack ``1,5,+''. Finally, ``1,5,+'' is evaluated resulting in the
93 answer 6. For convenience, it's useful to write this set of
94 operations as:</p>
95 <pre>
96 1) 1,2,3,+,+ eval is 2,3,+ = 5 result is 1,5,+
97 2) 1,5,+ eval is 1,5,+ = 6 result is 6
98 3) 6</pre>
99 <p>Let's use that notation to conveniently solve some complex RPN expressions
100 with multiple logic operators:</p>
101 <pre>
102 1) 20,10,GT,10,20,IF eval is 20,10,GT = 1 result is 1,10,20,IF</pre>
103 <p>read the eval as pop ``20 is greater than 10'' so push 1</p>
104 <pre>
105 2) 1,10,20,IF eval is 1,10,20,IF = 10 result is 10</pre>
106 <p>read pop ``if 1 then 10 else 20'' so push 10. Only 10 is left so
107 10 is the answer.</p>
108 <p>Let's read a complex RPN expression that also has the traditional
109 multiplication operator:</p>
110 <pre>
111 1) 128,8,*,7000,GT,7000,128,8,*,IF eval 128,8,* result is 1024
112 2) 1024,7000,GT,7000,128,8,*,IF eval 1024,7000,GT result is 0
113 3) 0,128,8,*,IF eval 128,8,* result is 1024
114 4) 0,7000,1024,IF result is 1024</pre>
115 <p>Now let's go back to the first example of multiple logic operators,
116 but replace the value 20 with the variable ``input'':</p>
117 <pre>
118 1) input,10,GT,10,input,IF eval is input,10,GT ( lets call this A )</pre>
119 <p>Read eval as ``if input > 10 then true'' and replace ``input,10,GT''
120 with ``A'':</p>
121 <pre>
122 2) A,10,input,IF eval is A,10,input,IF</pre>
123 <p>read ``if A then 10 else input''. Now replace A with it's verbose
124 description again and--voila!--you have a easily readable description
125 of the expression:</p>
126 <pre>
127 if input > 10 then 10 else input</pre>
128 <p>Finally, let's go back to the first most complex example and replace
129 the value 128 with ``input'':</p>
130 <pre>
131 1) input,8,*,7000,GT,7000,input,8,*,IF eval input,8,* result is A</pre>
132 <p>where A is ``input * 8''</p>
133 <pre>
134 2) A,7000,GT,7000,input,8,*,IF eval is A,7000,GT result is B</pre>
135 <p>where B is ``if ((input * 8) > 7000) then true''</p>
136 <pre>
137 3) B,7000,input,8,*,IF eval is input,8,* result is C</pre>
138 <p>where C is ``input * 8''</p>
139 <pre>
140 4) B,7000,C,IF</pre>
141 <p>At last we have a readable decoding of the complex RPN expression with
142 a variable:</p>
143 <pre>
144 if ((input * 8) > 7000) then 7000 else (input * 8)</pre>
145 <p>
146 </p>
147 <hr />
148 <h1><a name="exercises">Exercises</a></h1>
149 <p>Exercise 1:</p>
150 <p>Compute ``3,2,*,1,+ and ''3,2,1,+,*`` by hand. Rewrite them in
151 traditional notation. Explain why they have different answers.</p>
152 <p>Answer 1:</p>
153 <pre>
154 3*2+1 = 7 and 3*(2+1) = 9. These expressions have
155 different answers because the altering of the plus and
156 times operators alter the order of their evaluation.</pre>
157 <p>Exercise 2:</p>
158 <p>One may be tempted to shorten the expression</p>
159 <pre>
160 input,8,*,56000,GT,56000,input,*,8,IF</pre>
161 <p>by removing the redundant use of ``input,8,*'' like so:</p>
162 <pre>
163 input,56000,GT,56000,input,IF,8,*</pre>
164 <p>Use traditional notation to show these expressions are not the same.
165 Write an expression that's equivalent to the first expression, but
166 uses the LE and DIV operators.</p>
167 <p>Answer 2:</p>
168 <pre>
169 if (input <= 56000/8 ) { input*8 } else { 56000 }
170 input,56000,8,DIV,LT,input,8,*,56000,IF</pre>
171 <p>Exercise 3:</p>
172 <p>Briefly explain why traditional mathematic notation requires the
173 use of parentheses. Explain why RPN notation does not require
174 the use of parentheses.</p>
175 <p>Answer 3:</p>
176 <pre>
177 Traditional mathematic expressions are evaluated by
178 doing multiplication and division first, then addition and
179 subtraction. Parentheses are used to force the evaluation of
180 addition before multiplication (etc). RPN does not require
181 parentheses because the ordering of objects on the stack
182 can force the evaluation of addition before multiplication.</pre>
183 <p>Exercise 4:</p>
184 <p>Explain why it was desirable for the RRDtool developers to implement
185 RPN notation instead of traditional mathematical notation.</p>
186 <p>Answer 4:</p>
187 <pre>
188 The algorithm that implements traditional mathematical
189 notation is more complex then algorithm used for RPN.
190 So implementing RPN allowed Tobias Oetiker to write less
191 code! (The code is also less complex and therefore less
192 likely to have bugs.)</pre>
193 <p>
194 </p>
195 <hr />
196 <h1><a name="author">AUTHOR</a></h1>
197 <p>Steve Rader <<a href="mailto:rader@wiscnet.net">rader@wiscnet.net</a>></p>
199 </body>
201 </html>