CALC(1)                                                   CALC(1)

     NAME
          calc - calculator language

     SYNOPSIS
          calc [ -s ] [ file ]

          calc [ -s ] [ expression ]

     DESCRIPTION
          Calc interprets a simple language for floating-point arith-
          metic with Limbo-like syntax and functions.

          If no file or expression is given calc interprets the stan-
          dard input.

          Calc input consists of expressions and statements. Expres-
          sions are evaluated and their results printed.  Statements,
          typically assignments and function definitions, produce no
          output unless they explicitly call print.

          Comments begin with # and extend to the end of the line as
          in Limbo.

          Numbers may have a base specified using C or Limbo syntax.

          Variable names have the usual syntax, including `_'.  They
          may be introduced without a declaration and have an initial
          default value of 0.0.

          The predefined variable degrees can be set to specify angles
          in degrees rather than radians in the trigonometric func-
          tions below. It is initially 0 (angles in radians by
          default).

          The predefined variable printbase can be set to any integer
          between 2 and 36 inclusive to specify the base of all values
          output.

          The constants e, Pi(π), Phi(φ), Gamma(γ), Infinity(∞), and
          Nan(NaN) are predefined.

          Expressions are formed with these Limbo-like operators,
          listed by decreasing precedence.

          ! ~ + - ++ --

          **

          * / % //

     Page 1                       Plan 9             (printed 4/18/24)

     CALC(1)                                                   CALC(1)

          + -

          << >>

          > >= < <= <->

          == != -> <-

          &  ↑

          ^

          |  ↓

          &&

          ||

          ? :

          = := += -= *= /=

          ,

          If the -s flag is given, a strict interpretation of the dec-
          laration rules are enforced - all variables must be declared
          and initialized first using the := operator. Otherwise unde-
          clared variables are declared and initialized to 0.0 in the
          current scope. In either case, := always forces a new decla-
          ration.

          The extra non-Limbo operators are factorial (! when post-
          fix), integer division (//), conditional (? and :) comma
          (,), logical equivalence (<->), implication (->), reverse
          implication (<-), nand (↑) and nor (↓).

          Unary operators, assignment operators, **, ? and : are right
          associative as usual.

          The comma operator may be replaced by white space in expres-
          sions.

          Built in functions are abs, acos, acosh, asin, asinh, atan,
          atanh, atan2, cbrt, ceiling, cos, cosh, erf, exp, floor,
          frac, gamma(Γ), int, log, log10, max, min, pow, rand, round,
          sign, sin, sinh, sqrt, tan, and tanh.

          Functions of one argument may be written without brackets:

               sin 45
               sqrt 2

     Page 2                       Plan 9             (printed 4/18/24)

     CALC(1)                                                   CALC(1)

          These behave as unary operators with the highest precedence.

          Sum and product operators are available using sigma (Σ) and
          pi (Π).  For example

               sigma(i = 0, 100, 1/i!)

          gives the value 2.7182818284590455 .

          Simple definite integration can be done:

               integral(x = -1.96, 1.96, exp(-0.5*x*x)/sqrt(2*Pi))

          outputs 0.9500042096998785 .  ∫ may be used in place of
          integral.

          For the sake of completeness, the derivative of a function
          at a given point can be calculated:

               differential(x=1, x*x+5*x+6)

          gives 7.  Δ may be used in place of differential.

          There is limited support for the solution of equations.  For
          example

               solve(x**2-5*x+6==0)

          outputs the values 2 and 3. The value returned by solve is
          the largest of the roots. To specify the variable to solve
          for, if ambiguous, simply add it as a second parameter as
          in, for example,

               solve(x**2-5*x+6==y**3+z, x)

          This will substitute the current values of y and z and solve
          for x. To tune the solution process, the predefined vari-
          ables solvelimit (default value 100) and solvestep (default
          value 1) are available.  The former specifies the maximum
          absolute solution to search for. The latter specifies the
          interval increment to apply when searching for sign changes.

          Print prints a list of expressions that may include string
          constants such as "hello\n".

          Read reads in a list of values interactively. The list of
          variables to assign these values should follow.

          Other files may be read in using the Limbo include state-
          ment.

          Control flow statements are break, continue, exit, return,

     Page 3                       Plan 9             (printed 4/18/24)

     CALC(1)                                                   CALC(1)

          if-else, while, do-while, and for, with braces for grouping.

          The use of semi-colon and newline is optional.

          Functions are introduced by the keyword fn.

     EXAMPLE
          fn ack(a, b)
          {
               n = n+1
               if(a == 0)
                    return b+1;
               if(b == 0)
                    return ack(a-1, 1);
               return ack(a-1, ack(a, b-1));
          }

          for(i = 0; i < 4; i++)
               for(j = 0; j < 4; j++){
                    n = 0
                    print "ack(", i, ",", j, ")=", ack(i, j), "\n"
                    print n, " calls", "\n"
               }

     SOURCE
          /appl/cmd/calc.b

     SEE ALSO
          fc(1), math-intro(2)

     Page 4                       Plan 9             (printed 4/18/24)