MPE/iX Shell and Utilities Reference Manual, Vol 1

bc(1) MPE/iX Shell and Utilities bc(1)
An array is a list of values. Values in the list are called elements of the array. Each ele-
ment in an array is numbered, beginning at zero. Such a number is known as a subscript
or index of the array. Subscripts always appear in square brackets after the array. For
example, a[0] refers to element zero in the array a. If a subscript value is a floating
point number, the fractional part is discarded to make the subscript into an integer. For
example, the following expressions all refer to the same element:
a[3] a[3.2] a[3.999]
The maximum number of elements in a bc array is given by the configuration variable
{BC_DIM_MAX}. The valid array subscripts range from 0 to -1 inclusive. Unlike many
languages, you don’t need to declare the size of an array. Elements are created dynami-
cally as required, with an initial value of zero.
Since parentheses always follow function names and square brackets always follow array
names, bc can distinguish between the three types of names. Therefore, you can have vari-
ables, functions, and arrays with the same name. For example, foo may be a variable, while
foo() is a function and foo[] is an array.
Built-In Variables
bc has a number of built-in variables which are used to control various aspects of the inter-
preter. These are described in the following sections.
Scale
The scale value is the number of digits to be retained after the decimal point in arithmetic
operations. For example, if the scale is three, each calculation retains at least three digits after
the decimal point. This means that
5/3
has the value
1.666
If –l is specified, the scale is set to 20; otherwise the default scale is zero.
The variable scale holds the current scale value. To change scales, assign a new value to
scale,asin
scale = 5
Since scale is just a regular bc variable, it can be used in the full range of bc expressions.
The number of decimal places in the result of a calculation is affected not only by the scale,
but also by the number of decimal places in the operands of the calculation. This is discussed
in detail in the Operations section.
Commands and Utilities 1-47