Gri: RPN binary operators

  Chapters: 1: Introduction 2: Simple example 3: Invocation 4: Finer Control 5: X-Y Plots 6: Contour Plots 7: Image Plots 8: Examples 9: Gri Commands 10: Programming 11: Environment 12: Emacs Mode 13: History 14: Installation 15: Gri Bugs 16: Test Suite 17: Gri in Press 18: Acknowledgments 19: License Indices: Concepts Commands Variables | ### 10.9.4: Binary Operators Binary operators act on the top two items on the stack. Most binary operators replace two items on the stack with one item, e.g. ``{rpn 1 2 /}`' yields 0.5. However, a few binary operators replace one pair of items with a new pair of items, e.g. the ``xyusertocm`' operator replaces an (x,y) pair in user coordinates with an (xcm,ycm) pair in coordinates of centimeters on the page. The binary operators are illustrated below, in rough alphabetical order. `{rpn 3 2 +}` Add 2 to 3. `{rpn 3 2 -}` Subtract 2 from 3. `{rpn 3 2 *}` Multiply 3 by 2. `{rpn 3 2 /}` Divide 3 by 2. `{rpn 3 2 <}` Test whether 2 is less than 3, yielding 1. Note: this convention may be confusing to users who are familiar with HP calculators from decades past. Present-day calculators use this convention, but possibly older calculators used the reverse convention, using ``>`' where Gri uses ``<`'. `{rpn 3 2 <=}` Test whether 2 is less than or equal to 3. `{rpn 3 2 >}` Test whether 2 is greater than 3, yielding 0. `{rpn 3 2 >=}` Test whether 2 is greater than or equal to 3, yielding 0. `{rpn 3 2 ==}` Test whether 2 and 3 are equal, yielding 0. (Do not confuse this with the asignment operator ``=`', described next.) `{rpn 10 ".ten." =}` Assign the value ``10`' to the variable named ``.ten.`'. The variable name must be put in quotes, or else Gri will insert the value of the variable (if it exists) into the expression, instead of trying to assign to it. After the assignment is done, the stack is cleared of both the value and the variable name. For example, in the following code
      {rpn 3.1415 ".pi." =}
      show .pi.
  the first line evaluates to a blank line, and the second prints the value of Pi. NOTE: Do not confuse this with the ``==`' equality operator described above. `{rpn "hello" "\\greeting" =}` Assign the value ``"hello"`' to the synonym ``\greeting`'. See notes at the above item. `{rpn 3.14159 0 "x" =}` Assign the value Pi to the first element (at index ``0`') of the ``x`' column. All columns may be assigned to in this way, e.g. the following is a technique for plotting a quadratic function:
      .i. = 0
      .n. = 10
      while {rpn .i. .n. >}
          {rpn .i. .n. 1 - /     .i. "x" =}
          {rpn x .i. @ 2 power  .i. "y" =}
          .i. += 1
      end while
      draw curve
`{rpn 0 1 &}` Test whether 0 and 1 are both true, yielding 0. `{rpn 0 1 and}` Test whether 0 and 1 are both true, yielding 0. `{rpn y x area}` Calculate the area under the curve y=y(x). For details (see Manipulation of Columns etc). `{rpn 0 1 |}` Test whether either 0 or 1 is true, yielding 1. `{rpn 0 1 or}` Test whether either 0 or 1 is true, yielding 1. `{rpn 2 3 exch}` Exchange 2 and 3 on the stack, yielding ``3 2`' on the stack. (See also ``pop`' and ``dup`'.) `{rpn x 0 @}` Yields the value of the first number in the x column. A similar form also works for ``y`', etc. (see Manipulation of Columns etc). `{rpn 2 3 inf}` Pick the smaller of two values, yielding 3. (Opposite to ``sup`'.) `{rpn 2 3 power}` Take 2 to the 3rd power, yielding 8. Note: This convention may be confusing to users who are familiar with HP calculators from decades past. Present-day calculators use this convention, which they write as ``y^x`', but older calculators used the reverse convention, labelling the key ``x^y`'. `{rpn 2 3 remainder}` Calculate the remainder after dividing 2 by 3, yielding 2. The return value for ``{rpn A B remainder`'} is ``B - n * A`', where ``n`' is the quotient of ``A/B`', rounded towards zero to an integer. In this case, ``2/3`' rounds to an ``n`' value of zero, yielding 2 as the resulting remainder. `{rpn "heLLo" "s/L/l/g" sed}` Switch all instances of ``L`' into ``l`', yielding the string ``"hello"`' on the stack. This can be helpful for working with filenames, etc. The work is preformed with a system call to the ``sed`' utility (present on unix systems), and therefore this command will fail if ``sed`' is not installed, or if the OS cannot be contacted. `{rpn "file" ".dat" strcat}` Concatenate the two strings, yielding the string ``"file.dat"`'. `{rpn 2 3 sup}` Pick the larger of two values, yielding 3. (Opposite to ``inf`'.)