📄 bc.1
字号:
bc(1) Minix Programmer's Manual bc(1)
read ( )
The read function (an extension) will read a number from the
standard input, regardless of where the function occurs. Beware,
this can cause problems with the mixing of data and program in the
standard input. The best use for this function is in a previously
written program that needs input from the user, but never allows
program code to be input from the user. The value of the read
function is the number read from the standard input using the
current value of the variable ibase for the conversion base.
scale ( expression )
The value of the scale function is the number of digits after the
decimal point in the expression.
sqrt ( expression )
The value of the sqrt function is the square root of the expression.
If the expression is negative, a run time error is generated.
STATEMENTS
Statements (as in most algebraic languages) provide the sequencing of
expression evaluation. In bc statements are executed "as soon as
possible." Execution happens when a newline in encountered and there is
one or more complete statements. Due to this immediate execution,
newlines are very important in bc. In fact, both a semicolon and a
newline are used as statement separators. An improperly placed newline
will cause a syntax error. Because newlines are statement separators, it
is possible to hide a newline by using the backslash character. The
sequence "\<nl>", where <nl> is the newline appears to bc as whitespace
instead of a newline. A statement list is a series of statements
separated by semicolons and newlines. The following is a list of bc
statements and what they do: (Things enclosed in brackets ([]) are
optional parts of the statement.)
expression
This statement does one of two things. If the expression starts
with "<variable> <assignment> ...", it is considered to be an
assignment statement. If the expression is not an assignment
statement, the expression is evaluated and printed to the output.
After the number is printed, a newline is printed. For example,
"a=1" is an assignment statement and "(a=1)" is an expression that
has an embedded assignment. All numbers that are printed are
printed in the base specified by the variable obase. The legal
values for obase are 2 through BC_BASE_MAX. (See the section
LIMITS.) For bases 2 through 16, the usual method of writing
numbers is used. For bases greater than 16, bc uses a multi-
character digit method of printing the numbers where each higher
base digit is printed as a base 10 number. The multi-character
digits are separated by spaces. Each digit contains the number of
characters required to represent the base ten value of "obase-1".
Since numbers are of arbitrary precision, some numbers may not be
.\ 6
bc(1) Minix Programmer's Manual bc(1)
printable on a single output line. These long numbers will be split
across lines using the "\" as the last character on a line. The
maximum number of characters printed per line is 70. Due to the
interactive nature of bc printing a number cause the side effect of
assigning the printed value the the special variable last. This
allows the user to recover the last value printed without having to
retype the expression that printed the number. Assigning to last is
legal and will overwrite the last printed value with the assigned
value. The newly assigned value will remain until the next number
is printed or another value is assigned to last.
string
The string is printed to the output. Strings start with a double
quote character and contain all characters until the next double
quote character. All characters are take literally, including any
newline. No newline character is printed after the string.
print list
The print statement (an extension) provides another method of
output. The "list" is a list of strings and expressions separated
by commas. Each string or expression is printed in the order of the
list. No terminating newline is printed. Expressions are evaluated
and their value is printed and assigned the the variable last.
Strings in the print statement are printed to the output and may
contain special characters. Special characters start with the
backslash character (\). The special characters recognized by bc
are "b" (bell), "f" (form feed), "n" (newline), "r" (carriage
return), "t" (tab), and "\" (backslash). Any other character
following the backslash will be ignored. This still does not allow
the double quote character to be part of any string.
{ statement_list }
This is the compound statement. It allows multiple statements to be
grouped together for execution.
if ( expression ) then statement1 [else statement2]
The if statement evaluates the expression and executes statement1 or
statement2 depending on the value of the expression. If the
expression is non-zero, statement1 is executed. If statement2 is
present and the value of the expression is 0, then statement2 is
executed. (The else clause is an extension.)
while ( expression ) statement
The while statement will execute the statement while the expression
is non-zero. It evaluates the expression before each execution of
the statement. Termination of the loop is caused by a zero
expression value or the execution of a break statement.
.\ 7
bc(1) Minix Programmer's Manual bc(1)
for ( [expression1] ; [expression2] ; [expression3] ) statement
The for statement controls repeated execution of the statement.
Expression1 is evaluated before the loop. Expression2 is evaluated
before each execution of the statement. If it is non-zero, the
statement is evaluated. If it is zero, the loop is terminated.
After each execution of the statement, expression3 is evaluated
before the reevaluation of expression2. If expression1 or
expression3 are missing, nothing is evaluated at the point they
would be evaluated. If expression2 is missing, it is the same as
substituting the value 1 for expression2. (The optional expressions
are an extension. POSIX bc requires all three expressions.) The
following is equivalent code for the for statement:
expression1;
while (expression2) {
statement;
expression3;
}
break
This statement causes a forced exit of the most recent enclosing
while statement or for statement.
continue
The continue statement (an extension) causes the most recent
enclosing for statement to start the next iteration.
halt The halt statement (an extension) is an executed statement that
causes the bc processor to quit only when it is executed. For
example, "if (0 == 1) halt" will not cause bc to terminate because
the halt is not executed.
return
Return the value 0 from a function. (See the section on functions.)
return ( expression )
Return the value of the expression from a function. (See the
section on functions.)
PSEUDO STATEMENTS
These statements are not statements in the traditional sense. They are
not executed statements. Their function is performed at "compile" time.
limits
Print the local limits enforced by the local version of bc. This is
an extension.
quit When the quit statement is read, the bc processor is terminated,
regardless of where the quit statement is found. For example, "if
(0 == 1) quit" will cause bc to terminate.
.\ 8
bc(1) Minix Programmer's Manual bc(1)
warranty
Print a longer warranty notice. This is an extension.
FUNCTIONS
Functions provide a method of defining a computation that can be executed
later. Functions in bc always compute a value and return it to the
caller. Function definitions are "dynamic" in the sense that a function
is undefined until a definition is encountered in the input. That
definition is then used until another definition function for the same
name is encountered. The new definition then replaces the older
definition. A function is defined as follows:
define name ( parameters ) { newline
auto_list statement_list }
A function call is just an expression of the form "name(parameters)".
Parameters are numbers or arrays (an extension). In the function
definition, zero or more parameters are defined by listing their names
separated by commas. Numbers are only call by value parameters. Arrays
are only call by variable. Arrays are specified in the parameter
definition by the notation "name[]". In the function call, actual
parameters are full expressions for number parameters. The same notation
is used for passing arrays as for defining array parameters. The named
array is passed by variable to the function. Since function definitions
are dynamic, parameter numbers and types are checked when a function is
called. Any mismatch in number or types of parameters will cause a
runtime error. A runtime error will also occur for the call to an
undefined function.
The auto_list is an optional list of variables that are for "local" use.
The syntax of the auto list (if present) is "auto name, ... ;". (The
semicolon is optional.) Each name is the name of an auto variable.
Arrays may be specified by using the same notation as used in parameters.
These variables have their values pushed onto a stack at the start of the
function. The variables are then initialized to zero and used throughout
the execution of the function. At function exit, these variables are
popped so that the original value (at the time of the function call) of
these variables are restored. The parameters are really auto variables
that are initialized to a value provided in the function call. Auto
variables are different than traditional local variables in the fact that
if function A calls function B, B may access function A's auto variables
by just using the same name, unless function B has called them auto
variables. Due to the fact that auto variables and parameters are pushed
onto a stack, bc supports recursive functions.
The function body is a list of bc statements. Again, statements are
separated by semicolons or newlines. Return statements cause the
termination of a function and the return of a value. There are two
versions of the return statement. The first form, "return", returns the
value 0 to the calling expression. The second form, "return ( expression
)", computes the value of the expression and returns that value to the
.\ 9
bc(1) Minix Programmer's Manual bc(1)
calling expression. There is an implied "return (0)" at the end of every
function. This allows a function to terminate and return 0 without an
explicit return statement.
Functions also change the usage of the variable ibase. All constants in
the function body will be converted using the value of ibase at the time
of the function call. Changes of ibase will be ignored during the
execution of the function except for the standard function read, which
will always use the current value of ibase for conversion of numbers.
MATH LIBRARY
If bc is invoked with the -l option, a math library is preloaded and the
default scale is set to 20. The math functions will calculate their
results to the scale set at the time of their call. The math library
defines the following functions:
s (x)
The sine of x in radians.
c (x)
The cosine of x in radians.
a (x)
The arctangent of x.
l (x)
The natural logarithm of x.
e (x)
The exponential function of raising e to the value x.
j (n,x)
The bessel function of integer order n of x.
EXAMPLES
In /bin/sh, the following will assign the value of "pi" to the shell
variable pi.
pi=$(echo "scale=10; 4*a(1)" | bc -l)
The following is the definition of the exponential function used in the
math library. This function is written in POSIX bc.
scale = 20
/* Uses the fact that e^x = (e^(x/2))^2
When x is small enough, we use the series:
e^x = 1 + x + x^2/2! + x^3/3! + ...
*/
define e(x) {
.\ 10
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -