bc: Basic Expressions

1 
1 3.2 Basic Expressions
1 =====================
1 
1 In the following descriptions of legal expressions, "expr" refers to a
1 complete expression and "VAR" refers to a simple or an array variable.
1 A simple variable is just a
1 
1    NAME
1 
1    and an array variable is specified as
1 
1    NAME[EXPR]
1 
1    Unless specifically mentioned the scale of the result is the maximum
1 scale of the expressions involved.
1 
1 '- expr'
1      The result is the negation of the expression.
1 
1 '++ VAR'
1      The variable is incremented by one and the new value is the result
1      of the expression.
1 
1 '-- VAR'
1      The variable is decremented by one and the new value is the result
1      of the expression.
1 
1 'VAR ++'
1      The result of the expression is the value of the variable and then
1      the variable is incremented by one.
1 
1 'VAR --'
1      The result of the expression is the value of the variable and then
1      the variable is decremented by one.
1 
1 'expr + expr'
1      The result of the expression is the sum of the two expressions.
1 
1 'expr - expr'
1      The result of the expression is the difference of the two
1      expressions.
1 
1 'expr * expr'
1      The result of the expression is the product of the two expressions.
1      If a and b are the scales of the two expressions, then the scale of
1      the result is: min(a+b,max(SCALE,a,b))
1 
1 'expr / expr'
1      The result of the expression is the quotient of the two
1      expressions.  The scale of the result is the value of the variable
1      'scale'
1 
1 'expr % expr'
1      The result of the expression is the "remainder" and it is computed
1      in the following way.  To compute a%b, first a/b is computed to
1      SCALE digits.  That result is used to compute a-(a/b)*b to the
1      scale of the maximum of SCALE+scale(b) and scale(a).  If SCALE is
1      set to zero and both expressions are integers this expression is
1      the integer remainder function.
1 
1 'expr ^ expr'
1      The result of the expression is the value of the first raised to
1      the second.  The second expression must be an integer.  (If the
1      second expression is not an integer, a warning is generated and the
1      expression is truncated to get an integer value.)  The scale of the
1      result is SCALE if the exponent is negative.  If the exponent is
1      positive the scale of the result is the minimum of the scale of the
1      first expression times the value of the exponent and the maximum of
1      SCALE and the scale of the first expression.  (e.g.  scale(a^b) =
1      min(scale(a)*b, max(SCALE, scale(a))).)  It should be noted that
1      expr^0 will always return the value of 1.
1 
1 '( expr )'
1      This alters the standard precedence to force the evaluation of the
1      expression.
1 
1 'VAR = expr'
1      The variable is assigned the value of the expression.
1 
1 'VAR <op>= expr'
1      This is equivalent to "VAR = VAR <op> expr" with the exception that
1      the "VAR" part is evaluated only once.  This can make a difference
1      if "VAR" is an array.
1