Code

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