📄 bc.1
字号:
.PPThe following is the definition of the exponential function used in themath library. This function is written in POSIX \fBbc\fR..nf.RS\fBscale = 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) { 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 }}\fR.RE.fi.PPThe following is code that uses the extended features of \fBbc\fR toimplement a simple program for calculating checkbook balances. Thisprogram is best kept in a file so that it can be used many times without having to retype it at every use..nf.RS\fBscale=2print "\enCheck book program!\en"print " Remember, deposits are negative transactions.\en"print " Exit by a 0 transaction.\en\en"print "Initial balance? "; bal = read()bal /= 1print "\en"while (1) { "current balance = "; bal "transaction? "; trans = read() if (trans == 0) break; bal -= trans bal /= 1}quit\fR.RE.fi.PPThe following is the definition of the recursive factorial function..nf.RS\fBdefine f (x) { if (x <= 1) return (1); return (f(x-1) * x);}\fR.RE.fi.SS DIFFERENCESThis version of .B bcwas implemented from the POSIX P1003.2/D11 draft and containsseveral differences and extensions relative to the draft andtraditional implementations.It is not implemented in the traditional way using.I dc(1).This version is a single process which parses and runs a byte codetranslation of the program. There is an "undocumented" option (-c)that causes the program to output the byte code tothe standard output instead of running it. It was mainly used fordebugging the parser and preparing the math library..PPA major source of differences isextensions, where a feature is extended to add more functionality andadditions, where new features are added. The following is the list of differences and extensions..IP LANG 11nThis version does not conform to the POSIX standard in the processingof the LANG environment variable and all environment variables startingwith LC_..IP namesTraditional and POSIX.B bchave single letter names for functions, variables and arrays. They havebeen extended to be multi-character names that start with a letter andmay contain letters, numbers and the underscore character..IP StringsStrings are not allowed to contain NUL characters. POSIX says all charactersmust be included in strings..IP lastPOSIX \fBbc\fR does not have a \fBlast\fR variable. Some implementationsof \fBbc\fR use the period (.) in a similar way. .IP comparisonsPOSIX \fBbc\fR allows comparisons only in the if statement, the whilestatement, and the second expression of the for statement. Also, onlyone relational operation is allowed in each of those statements..IP "if statement, else clause"POSIX \fBbc\fR does not have an else clause..IP "for statement"POSIX \fBbc\fR requires all expressions to be present in the for statement..IP "&&, ||, !"POSIX \fBbc\fR does not have the logical operators..IP "read function"POSIX \fBbc\fR does not have a read function..IP "print statement"POSIX \fBbc\fR does not have a print statement ..IP "continue statement"POSIX \fBbc\fR does not have a continue statement..IP "array parameters"POSIX \fBbc\fR does not have array parameters. Other implementationsof \fBbc\fR may have call by value array parameters..IP "=+, =-, =*, =/, =%, =^"POSIX \fBbc\fR does not require these "old style" assignment operators tobe defined. This version may allow these "old style" assignments. Usethe limits statement to see if the installed version supports them. Ifit does support the "old style" assignment operators, the statement"a =- 1" will decrement \fBa\fR by 1 instead of setting \fBa\fR to thevalue -1..IP "spaces in numbers"Other implementations of \fBbc\fR allow spaces in numbers. For example,"x=1 3" would assign the value 13 to the variable x. The same statementwould cause a syntax error in this version of \fBbc\fR..IP "errors and execution"This implementation varies from other implementations in terms of whatcode will be executed when syntax and other errors are found in theprogram. If a syntax error is found in a function definition, errorrecovery tries to find the beginning of a statement and continue toparse the function. Once a syntax error is found in the function, thefunction will not be callable and becomes undefined.Syntax errors in the interactive execution code will invalidate thecurrent execution block. The execution block is terminated by anend of line that appears after a complete sequence of statements.For example, .nf.RSa = 1b = 2.RE.fihas two execution blocks and.nf.RS{ a = 1 b = 2 }.RE.fihas one execution block. Any runtime error will terminate the executionof the current execution block. A runtime warning will not terminate thecurrent execution block..IP "Interrupts"During an interactive session, the SIGINT signal (usually generated bythe control-C character from the terminal) will cause execution of thecurrent execution block to be interrupted. It will display a "runtime"error indicating which function was interrupted. After all runtimestructures have been cleaned up, a message will be printed to notify theuser that \fBbc\fR is ready for more input. All previously defined functionsremain defined and the value of all non-auto variables are the value atthe point of interruption. All auto variables and function parametersare removed during theclean up process. During a non-interactivesession, the SIGINT signal will terminate the entire run of \fBbc\fR..SS LIMITSThe following are the limits currently in place for this .B bcprocessor. Some of them may have been changed by an installation.Use the limits statement to see the actual values..IP BC_BASE_MAXThe maximum output base is currently set at 999. The maximum input baseis 16..IP BC_DIM_MAXThis is currently an arbitrary limit of 65535 as distributed. Yourinstallation may be different..IP BC_SCALE_MAXThe 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_MAXdigits..IP BC_STRING_MAXThe limit on the number of characters in a string is INT_MAX characters..IP exponentThe value of the exponent in the raise operation (^) is limited to LONG_MAX..IP multiplyThe multiply routine may yield incorrect results if a numberhas more than LONG_MAX / 90 total digits. For 32 bit longs, this number is23,860,929 digits..IP "code size"Each function and the "main" program are limited to 10240 bytes ofcompiled byte code each. This limit (BC_MAX_SEGS) can be easily changedto have more than 10 segments of 1024 bytes..IP "variable names"The current limit on the number of unique names is 32767 for each ofsimple variables, arrays and functions..SH FILESIn most installations, \fBbc\fR is completely self-contained.Where executable size is of importance or the C compiler doesnot deal with very long strings, \fBbc\fR will readthe standard math library from the file /usr/local/lib/libmath.b.(The actual location may vary. It may be /lib/libmath.b.).SH DIAGNOSTICSIf any file on the command line can not be opened, \fBbc\fR will reportthat the file is unavailable and terminate. Also, there are compileand run time diagnostics that should be self-explanatory..SH BUGSError recovery is not very good yet..SH AUTHOR.nfPhilip A. Nelsonphil@cs.wwu.edu.fi.SH ACKNOWLEDGEMENTSThe author would like to thank Steve Sommars (sesv@iwtsf.att.com) forhis extensive help in testing the implementation. Many great suggestionswere given. This is a much better product due to his involvement.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -