MPE/iX Shell and Utilities Reference Manual, Vol 1

bc(1) MPE/iX Shell and Utilities bc(1)
You can use the following functions if –l is specified on the command line. If it is not, the
function names are not recognized. There are two names for each function: a full name, and a
single character name for compatibility with POSIX.2. The full names are the same as the
equivalent functions in the standard C math library.
atan(expression)or a(expression)
calculates the arctangent of expression, returning an angle in radians.
cos(expression)or c(expression)
calculates the cosine of expression, where expression is an angle in radians.
exp(expression)or e(expression)
calculates the exponential of expression (that is, the value e to the power of expression).
jn(integer,expression)or j(integer,expression)
calculates the Bessel function of expression, with order integer.
log(expression)or l(expression)
calculates the natural logarithm of expression.
sin(expression)or s(expression)
calculates the sine of expression, where expression is an angle in radians.
EXAMPLES
This sections provides some examples of how to use the bc language to accomplish various
things.
Here is a simple function to calculate the sales tax on a purchase. The amount of the purchase
is given by purchase, and the amount of the sales tax (in per cent) is given by tax.
define sales_tax(purchase,tax) {
auto old_scale
scale = 2
tax = purchase*(tax/100)
scale = old_scale
return (tax)
}
For example,
sales_tax(23.99,6)
calculates 6% tax on a purchase of $23.99. The function temporarily sets the scale value to 2
so that the monetary figures have two figures after the decimal point. Remember that bc trun-
cates calculations instead of rounding, so some accuracy may be lost. It is better to use one
more digit than needed and perform the rounding at the end. The round2() function, shown
later in this section, rounds a number to two decimal places.
1-60 Commands and Utilities