User Commandsbc(1)

bc - arbitrary precision arithmetic language

/usr/bin/bc[-c] [-l] [file].../usr/xpg6/bin/bc[-c] [-l] [file]...

Thebcutility implements an arbitrary precision calculator. It takes input from any files given, then reads from the standard input. If the standard input and standard output tobcare attached to a terminal, the invocation ofbcisinteractive, causing behavioral constraints described in the following sections.bcprocesses a language that resembles C and is a preprocessor for the desk calculator programdc, which it invokes automatically unless the-coption is specified. In this case thedcinput is sent to the standard output instead.

The syntax forbcprograms is as follows:LMeans a lettera-z,EMeans an expression: a (mathematical or logical) value, an operand that takes a value, or a combination of operands and operators that evaluates to a value,SMeans a statement.CommentsEnclosed in/*and*/.Names(Operands)Simple variables:L. Array elements:L[E] (up toBC_DIM_MAXdimensions). The wordsibase,obase(limited toBC_BASE_MAX), andscale(limited toBC_SCALE_MAX).OtherOperandsArbitrarily long numbers with optional sign and decimal point. Strings of fewer thanBC_STRING_MAXcharacters, between double quotes (").(E)sqrt(E)Square root SunOS 5.11 Last change: 29 Aug 2003 1 User Commandsbc(1)length(E)Number of significant decimal digits.scale(E)Number of digits right of decimal point.L(E, ... ,E)Operators+-*/%^(%is remainder;^is power)++--(prefix and postfix; apply to names)==<=>=!=<>==+=-=*=/=%=^StatementsE{S;...;S}if(E)Swhile(E)Sfor(E;E;E)Snull statementbreakquit.stringFunctionDefinitionsdefineL(L,...,L){autoL,...,LS;...Sreturn(E)}SunOS 5.11 Last change: 29 Aug 2003 2 User Commandsbc(1)Functionsin-lMath Librarys(x)sinec(x)cosinee(x)exponentiall(x)loga(x)arctangentj(n,x) Bessel function All function arguments are passed by value. The value of a statement that is an expression is printed unless the main operator is an assignment. Either semicolons or new-lines may separate statements. Assignment toscaleinfluences the number of digits to be retained on arithmetic operations in the manner ofdc. Assignments toibaseorobaseset the input and output number radix respectively. The same letter may be used as an array, a function, and a simple variable simultaneously. All variables are global to the program.autovariables are stacked during function calls. When using arrays as function arguments or defining them as automatic variables, empty square brackets must fol- low the array name.

The following operands are supported:-cCompiles only. The output isdccommands that are sent to the standard output./usr/bin/bc-lDefines the math functions and initializesscaleto20, instead of the default zero./usr/xpg6/bin/bcSunOS 5.11 Last change: 29 Aug 2003 3 User Commandsbc(1)-lDefines the math functions and initializesscaleto20, instead of the default zero. All math results have the scale of20.

The following operands are supported:fileA pathname of a text file containingbcprogram statements. After all cases offilehave been read,bcreads the standard input.

Example1Setting the precision of a variable In the shell, the following assigns an approximation of the first ten digits ofnto the variablex:x=$(printf"%s\n"'scale=10;104348/33215'|bc)Example2Defining a computing function Defines a function to compute an approximate value of the exponential function:scale=20definee(x){autoa,b,c,i,sa=1b=1s=1for(i=1;1==1;i++){a=a*xb=b*ic=a/bif(c==0)return(s)s=s+c}}Example3Printing the approximate values of the function SunOS 5.11 Last change: 29 Aug 2003 4 User Commandsbc(1)Prints approximate values of the exponential function of the first ten integers:for(i=1;i<=10;i++)e(i)orfor(i=1;i<=10;++i){e(i)}

Seeenviron(5)for descriptions of the following environment variables that affect the execution ofbc:LANG,LC_ALL,LC_CTYPE,LC_MESSAGES, andNLSPATH.

The following exit values are returned:0All input files were processed successfully.unspecifiedAn error occurred.

/usr/lib/lib.bmathematical library/usr/include/limits.hto define BC_ parameters

Seeattributes(5)for descriptions of the following attri- butes: SunOS 5.11 Last change: 29 Aug 2003 5 User Commandsbc(1)____________________________________________________________ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |_____________________________|_____________________________|| Availability | SUNWesu | |_____________________________|_____________________________|| Interface Stability | Standard | |_____________________________|_____________________________|

dc(1),awk(1),attributes(5),environ(5),standards(5)

Thebccommand does not recognize the logical operators&&and||. Theforstatement must have all three expressions (E's). SunOS 5.11 Last change: 29 Aug 2003 6

Man(1) output converted with man2html

Schily's Home