📄 bc.1
字号:
bc(1) Minix Programmer's Manual bc(1)
auto a, d, e, f, i, m, v, z
/* Check the sign of x. */
if (x<0) {
m = 1
x = -x
}
/* Precondition x. */
z = scale;
scale = 4 + z + .44*x;
while (x > 1) {
f += 1;
x /= 2;
}
/* Initialize the variables. */
v = 1+x
a = x
d = 1
for (i=2; 1; i++) {
e = (a *= x) / (d *= i)
if (e == 0) {
if (f>0) while (f--) v = v*v;
scale = z
if (m) return (1/v);
return (v/1);
}
v += e
}
}
The following is code that uses the extended features of bc to implement
a simple program for calculating checkbook balances. This program is
best kept in a file so that it can be used many times without having to
retype it at every use.
scale=2
print "\nCheck book program!\n"
print " Remember, deposits are negative transactions.\n"
print " Exit by a 0 transaction.\n\n"
print "Initial balance? "; bal = read()
bal /= 1
print "\n"
while (1) {
"current balance = "; bal
"transaction? "; trans = read()
.\ 11
bc(1) Minix Programmer's Manual bc(1)
if (trans == 0) break;
bal -= trans
bal /= 1
}
quit
The following is the definition of the recursive factorial function.
define f (x) {
if (x <= 1) return (1);
return (f(x-1) * x);
}
DIFFERENCES
This version of bc was implemented from the POSIX P1003.2/D11 draft and
contains several differences and extensions relative to the draft and
traditional implementations. It is not implemented in the traditional
way using dc(1). This version is a single process which parses and runs a
byte code translation of the program. There is an "undocumented" option
(-c) that causes the program to output the byte code to the standard
output instead of running it. It was mainly used for debugging the
parser and preparing the math library.
A major source of differences is extensions, where a feature is extended
to add more functionality and additions, where new features are added.
The following is the list of differences and extensions.
LANG This version does not conform to the POSIX standard in the
processing of the LANG environment variable and all
environment variables starting with LC_.
names Traditional and POSIX bc have single letter names for
functions, variables and arrays. They have been extended to
be multi-character names that start with a letter and may
contain letters, numbers and the underscore character.
Strings Strings are not allowed to contain NUL characters. POSIX says
all characters must be included in strings.
last POSIX bc does not have a last variable. Some implementations
of bc use the period (.) in a similar way.
comparisons
POSIX bc allows comparisons only in the if statement, the
while statement, and the second expression of the for
statement. Also, only one relational operation is allowed in
each of those statements.
.\ 12
bc(1) Minix Programmer's Manual bc(1)
if statement, else clause
POSIX bc does not have an else clause.
for statement
POSIX bc requires all expressions to be present in the for
statement.
&&, ||, ! POSIX bc does not have the logical operators.
read function
POSIX bc does not have a read function.
print statement
POSIX bc does not have a print statement .
continue statement
POSIX bc does not have a continue statement.
array parameters
POSIX bc does not have array parameters. Other
implementations of bc may have call by value array parameters.
=+, =-, =*, =/, =%, =^
POSIX bc does not require these "old style" assignment
operators to be defined. This version may allow these "old
style" assignments. Use the limits statement to see if the
installed version supports them. If it does support the "old
style" assignment operators, the statement "a =- 1" will
decrement a by 1 instead of setting a to the value -1.
spaces in numbers
Other implementations of bc allow spaces in numbers. For
example, "x=1 3" would assign the value 13 to the variable x.
The same statement would cause a syntax error in this version
of bc.
errors and execution
This implementation varies from other implementations in terms
of what code will be executed when syntax and other errors are
found in the program. If a syntax error is found in a
function definition, error recovery tries to find the
beginning of a statement and continue to parse the function.
Once a syntax error is found in the function, the function
will not be callable and becomes undefined. Syntax errors in
the interactive execution code will invalidate the current
execution block. The execution block is terminated by an end
of line that appears after a complete sequence of statements.
For example,
a = 1
b = 2
.\ 13
bc(1) Minix Programmer's Manual bc(1)
has two execution blocks and
{ a = 1
b = 2 }
has one execution block. Any runtime error will terminate the
execution of the current execution block. A runtime warning
will not terminate the current execution block.
Interrupts During an interactive session, the SIGINT signal (usually
generated by the control-C character from the terminal) will
cause execution of the current execution block to be
interrupted. It will display a "runtime" error indicating
which function was interrupted. After all runtime structures
have been cleaned up, a message will be printed to notify the
user that bc is ready for more input. All previously defined
functions remain defined and the value of all non-auto
variables are the value at the point of interruption. All
auto variables and function parameters are removed during the
clean up process. During a non-interactive session, the
SIGINT signal will terminate the entire run of bc.
LIMITS
The following are the limits currently in place for this bc processor.
Some of them may have been changed by an installation. Use the limits
statement to see the actual values.
BC_BASE_MAX
The maximum output base is currently set at 999. The maximum
input base is 16.
BC_DIM_MAX This is currently an arbitrary limit of 65535 as distributed.
Your installation may be different.
BC_SCALE_MAX
The number of digits after the decimal point is limited to
INT_MAX digits. Also, the number of digits before the decimal
point is limited to INT_MAX digits.
BC_STRING_MAX
The limit on the number of characters in a string is INT_MAX
characters.
exponent The value of the exponent in the raise operation (^) is
limited to LONG_MAX.
multiply The multiply routine may yield incorrect results if a number
has more than LONG_MAX / 90 total digits. For 32 bit longs,
this number is 23,860,929 digits.
.\ 14
bc(1) Minix Programmer's Manual bc(1)
code size Each function and the "main" program are limited to 10240
bytes of compiled byte code each. This limit (BC_MAX_SEGS)
can be easily changed to have more than 10 segments of 1024
bytes.
variable names
The current limit on the number of unique names is 32767 for
each of simple variables, arrays and functions.
FILES
In most installations, bc is completely self-contained. Where executable
size is of importance or the C compiler does not deal with very long
strings, bc will read the standard math library from the file
/usr/local/lib/libmath.b. (The actual location may vary. It may be
/lib/libmath.b.)
DIAGNOSTICS
If any file on the command line can not be opened, bc will report that
the file is unavailable and terminate. Also, there are compile and run
time diagnostics that should be self-explanatory.
BUGS
Error recovery is not very good yet.
AUTHOR
Philip A. Nelson
phil@cs.wwu.edu
ACKNOWLEDGEMENTS
The author would like to thank Steve Sommars (sesv@iwtsf.att.com) for his
extensive help in testing the implementation. Many great suggestions
were given. This is a much better product due to his involvement.
.\ 15
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -