290 lines · 3.7 KB · Text
RawDownload
.TH BC 1 .SH NAMEbc \- arbitrary-precision arithmetic language.SH SYNOPSIS.B bc[.B -cdls][.I file ...].SH DESCRIPTION.I Bcis an interactive processor for a language that resemblesC but provides arithmetic on numbers of arbitrary length with upto 100 digits right of the decimal point.It takes input from any files given, then readsthe standard input..PPThe.B -doption enables debugging output.The.B -loption stands for the nameof an arbitrary precision math library.The.B -soption suppresses the automatic displayof calculation results; all output is via the.B printcommand..PPThe following syntax for .I bcprograms is like that of C;.I Lmeans letter.BR a - z ,.I Emeans expression,.I Smeans statement..TF length(E).TPLexical.RS.HPcomments are enclosed in.B /* */.HPnewlines end statements.RE.TPNames.IPsimple variables:.I L.brarray elements:.IB L [ E ].brThe words.BR ibase ,.BR obase ,and.B scale.TPOther operands.IParbitrarily long numbers with optional sign and decimal point..RS.TP.BI ( E ).TP.BI sqrt( E ).TP.BI length( E )number of significant decimal digits.TP.BI scale( E )number of digits right of decimal point.TP.IB L ( E , ... ,\fIE\fP)function call.RE.TPOperators.RS.HP.B "+  -  *  /  %  ^\ ".RB ( %is remainder;.B ^is power).HP.B "++  --\ ".TP.B "==  <=  >=  !=  <  >".TP.B "=  +=  -=  *=  /=  %=  ^=".RE.TPStatements.RS.br.I E.br.B {.I S.B ;\&....B ;.I S.B }.br.B "print".I E.br.B "if (".I E.B ).I S.br.B "while (".I E.B ).I S.br.B "for (".I E.B ;.I E.B ;.I E.B ")".I  S.brnull statement.br.B break.br.B quit.br\fL"\fRtext\fL"\fR.RE.TPFunction definitions.RS.br.B define.I L.B (.I L.B ,\&....B ,.I L.B ){.PD0.br.B auto.I L.B ,\&....B ,.I L.br.I S.B ;\&....B ;.I  S.br.B return.I E.LP.B }.RE.TPFunctions in .B -lmath library.RS.TP.BI s( x )sine.TP.BI c( x )cosine.TP.BI e( x )exponential.TP.BI l( x )log.TP.BI a( x )arctangent.TP.BI j( "n, x" )Bessel function.RE.PP.DTAll function arguments are passed by value..PD.PPThe value of an expression at the top level is printedunless the main operator is an assignment or the.B -scommand line argument is given.Text in quotes, which may include newlines, is always printed.Either semicolons or newlines may separate statements.Assignment to.B scaleinfluences the number of digits to be retained on arithmeticoperations in the manner of.IR dc (1).Assignments to.B ibaseor.B obaseset the input and output number radix respectively..PPThe same letter may be used as an array, a function,and a simple variable simultaneously.All variables are global to the program.Automatic variables are pushed down during function calls.In a declaration of an array as a function argumentor automatic variableempty square brackets must follow the array name..PP.I Bcis actually a preprocessor for.IR dc (1),which it invokes automatically, unless the.B -c(compile only)option is present.In this case the.I dcinput is sent to the standard output instead..SH EXAMPLEDefine a function to compute an approximate value ofthe exponential.Use it to print 10 values.(The exponential function in the library gives better answers.).PP.EXscale = 20define e(x) {	auto a, b, c, i, s	a = 1	b = 1	s = 1	for(i=1; 1; i++) {		a *= x		b *= i		c = a/b		if(c == 0) return s		s += c	}}for(i=1; i<=10; i++) print e(i).EE.SH FILES.B /sys/lib/bclibmathematical library.SH SOURCE.B /sys/src/cmd/bc.y.SH "SEE ALSO".IR dc (1), .IR hoc (1).SH BUGSNo.LR && ,.LR || ,or.L !operators..PPA.L forstatement must have all three.LR E s..PPA.L quitis interpreted when read, not when executed.