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