[rrd-users] Re: RPN Tutorial with logical operators (AND/OR)?

Alex van den Bogaerdt alex at ergens.op.het.net
Sun May 2 23:43:01 MEST 2004


On Sun, May 02, 2004 at 05:26:41PM +0200, Laurent LEVIER wrote:

> I know * is the multiplication operator, but when you read this:
>    "CDEF:C2=in,$L2,LT,in,$L1,GE,*,in,$L1,-,0,IF,in,$L2,GT,$L1,0,IF,+" \
> I think the 1st algorithm is: IF in<L2 AND in>=L1 then C2=in-L1 else C2=0.
> 
> I dont understand what this * sign is doing here.

This is basic boolean algebra.  If the following is not enough, search for
boolean algebra on the Internet.


If "false" equals zero, and "true" equals any number but zero (usually one),
then the multiplication table is as follows:

AND:
A  B    result "A and B"

0  0    0*0 = 0 -> false
0  1    0*1 = 0 -> false
1  0    1*0 = 0 -> false
1  1    1*1 = 1 -> true
 

While I'm at it, here are some other boolean functions:

OR:
A  B    result "A or B"

0  0    0+0 = 0 -> false
0  1    0+1 = 1 -> true
1  0    1+0 = 1 -> true
1  1    1+1 = 2 -> true    <-- any number but zero!


XOR (one, but not both, true):

result = (A or B) and not (A and B)
result = (A and not B) or (B and not A)
result = if (A and B) then false else (A or B)

You could also use the method of checking against exactly one
as Hugo described.  When you do, make SURE the input is what
you expect.  Consider the following table:

A B
0 0   0+0 = 0 -> false
0 1   0+1 = 1 -> true
2 0   2+0 = 2 -> true
2 1   2+1 = 3 -> true

If you'd check the outcome against "1" (one), you'd miss the second true!
Also, if you'd check against "equal to two" in stead of "more than one",
you'd produce false results for the last true.

IMHO it is best to check XOR like this:

   result=A,B,AND,false,A,B,OR,IF

   (note: "AND", "OR" and "false" are used for clarity; fill in the
    appropriate stuff: "*", "+" and "0").

which would produce the following stack processing:

1:  <empty>
2:  A
3:  A,B
4:  <result of A and B>
5:  <result of A and B>,false
6:  <result of A and B>,false,A
7:  <result of A and B>,false,A,B
8:  <result of A and B>,false,<result of A or B>
9:  <result of  IF (A and B) then FALSE else result of (A or B)>

In other words:

   if (A is true) and (B is true)
   then return false
   else
      if (A is true) or (B is true)
      then return true
      else return false


NOT (inverse):

Usually not necessary.  Example: "if (something) then this else that"
In stead of calculating the inverse of "something", you could do:
"if (something) then that else this"

If you really want the NOT function, there are two possibilities:

#1: you can rely on the boolean to be "0" or "1"
#2: you cannot

1:  result=1-A              in RPN    result=1,A,-
2:  if (A) then 0 else 1    in RPN    result=A,0,1,IF

When processing booleans, be aware that RRDtool does not only numbers.
It can also store "NaN" and "Inf" into variables!

The result of "NaN" times "true" is "NaN" which is not false, so it is true
(last time I checked the source, that is).

HTH
Alex

--
Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/rrd-users
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the rrd-users mailing list