Copyright © 1997 The Open Group

bc - arbitrary-precision arithmetic language

`bc`

[-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 ofbcis considered to beinteractive, causing behavioural constraints described in the following sections.

Thebcutility supports theXBDspecification,Utility Syntax Guidelines.The following option is supported:

-l- (The letter ell.) Define the math functions and initialise
scaleto 20, instead of the default zero. See the EXTENDED DESCRIPTION section.

The following operands are supported:

file- A pathname of a text file containing
bcprogram statements. After all cases offilehave been read,bcwill read the standard input.

See the INPUT FILES section.

Input files must be text files containing a sequence of comments, statements and function definitions that will be executed as they are read.

The following environment variables affect the execution ofbc:

LANG- Provide a default value for the internationalisation variables that are unset or null. If
LANGis unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.LC_ALL- If set to a non-empty string value, override the values of all the other internationalisation variables.
LC_CTYPE- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files).
LC_MESSAGES- Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
NLSPATH- Determine the location of message catalogues for the processing of
LC_MESSAGES.

Default.

The output of thebcutility is controlled by the program read, and consists of zero or more lines containing the value of all executed expressions without assignments. The radix and precision of the output are controlled by the values of theobaseandscalevariables. See the EXTENDED DESCRIPTION section.

Used only for diagnostic messages.

None.

## Grammar

The grammar in this section and the lexical conventions in the following section together describe the syntax forbcprograms. The general conventions for this style of grammar are described in. A valid program can be represented as the non-terminal symbol programin the grammar. This formal syntax takes precedence over the preceding text syntax description.

%token EOF NEWLINE STRING LETTER NUMBER %token MUL_OP /* '*', '/', '%' */ %token ASSIGN_OP /* '=', '+=', '-=', '*=', '/=', '%=', '^=' */ %token REL_OP /* '==', '<=', '>=', '!=', '<', '>' */ %token INCR_DECR /* '++', '--' */ %token Define Break Quit Length /* 'define', 'break', 'quit', 'length' */ %token Return For If While Sqrt /* 'return', 'for', 'if', 'while', 'sqrt' */ %token Scale Ibase Obase Auto /* 'scale', 'ibase', 'obase', 'auto' */ %start program

%% program : EOF | input_item program ; input_item : semicolon_list NEWLINE | function ; semicolon_list : /* empty */ | statement | semicolon_list ';' statement | semicolon_list ';' ; statement_list : /* empty */ | statement | statement_list NEWLINE | statement_list NEWLINE statement | statement_list ';' | statement_list ';' statement ; statement : expression | STRING | Break | Quit | Return | Return '(' return_expression ')' | For '(' expression ';' relational_expression ';' expression ')' statement | If '(' relational_expression ')' statement | While '(' relational_expression ')' statement | '{' statement_list '}' ; function : Define LETTER '(' opt_parameter_list ')' '{' NEWLINE opt_auto_define_list statement_list '}' ; opt_parameter_list : /* empty */ | parameter_list ; parameter_list : LETTER | define_list ',' LETTER ; opt_auto_define_list : /* empty */ | Auto define_list NEWLINE | Auto define_list ';' ; define_list : LETTER | LETTER '[' ']' | define_list ',' LETTER | define_list ',' LETTER '[' ']' ; opt_argument_list : /* empty */ | argument_list ; argument_list : expression | argument_list ',' expression ; relational_expression : expression | expression REL_OP expression ; return_expression : /* empty */ | expression ; expression : named_expression | NUMBER | '(' expression ')' | LETTER '(' opt_argument_list ')' | '-' expression | expression '+' expression | expression '-' expression | expression MUL_OP expression | expression '^' expression | INCR_DECR named_expression | named_expression INCR_DECR | named_expression ASSIGN_OP expression | Length '(' expression ')' | Sqrt '(' expression ')' | Scale '(' expression ')' ; named_expression : LETTER | LETTER '[' expression ']' | Scale | Ibase | Obase ; ## Lexical Conventions in bc

The lexical conventions for bcprograms, with respect to the preceding grammar, are as follows:

- Except as noted,
bcrecognises the longest possible token or delimiter beginning at a given point.

- A comment consists of any characters beginning with the two adjacent characters /* and terminated by the next occurrence of the two adjacent characters */. Comments have no effect except to delimit lexical tokens.

- The character newline is recognised as the token
NEWLINE.

- The token
STRINGrepresents a string constant; it consists of any characters beginning with the double-quote character ( ) and terminated by another occurrence of the double-quote character. The value of the string is the sequence of all characters between, but not including, the two double-quote characters. All characters are taken literally from the input, and there is no way to specify a string containing a double-quote character. The length of the value of each string is limited to {BC_STRING_MAX} bytes.

- A blank character has no effect except as an ordinary character if it appears within a
STRINGtoken, or to delimit a lexical token other thanSTRING.

- The combination of a backslash character immediately followed by a newline character has no effect other than to delimit lexical tokens with the following exceptions:

- It is interpreted as the character sequence \newline in
STRINGtokens.

- It is ignored as part of a multi-line
NUMBERtoken.

- The token
NUMBERrepresents a numeric constant. It is recognised by the following grammar:

NUMBER : integer | '.' integer | integer '.' | integer '.' integer ; integer : digit | integer digit ; digit : 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F ;

- The value of a
NUMBERtoken is interpreted as a numeral in the base specified by the value of the internal registeribase(described below). Each of thedigitcharacters has the value from 0 to 15 in the order listed here, and the period character represents the radix point. The behaviour is undefined if digits greater than or equal to the value ofibaseappear in the token. However, note the exception for single-digit values being assigned toibaseandobasethemselves, inOperations in bc .

- The following keywords are recognised as tokens:

auto for length return sqrt break ibase obase scale while define if quit

- Any of the following characters occurring anywhere except within a keyword are recognised as the token
LETTER:`a b c d e f g h i j k l m n o p q r s t u v w x y z`

- The following single-character and two-character sequences are recognised as the token
ASSIGN_OP:`= += -= *= /= %= ^=`

- If an = character, as the beginning of a token, is followed by a - character with no intervening delimiter, the behaviour is undefined.

- The following single-characters are recognised as the token
MUL_OP:`* / %`

- The following single-character and two-character sequences are recognised as the token
REL_OP:`== <= >= != < >`

- The following two-character sequences are recognised as the token
INCR_DECR:`++ --`

- The following single characters are recognised as tokens whose names are the character:
`<newline> ( ) , + - ; [ ] ^ { }`

- The token
EOFwill be returned when the end of input is reached.

## Operations in bc

There are three kinds of identifiers: ordinary identifiers, array identifiers and function identifiers. All three types consist of single lower-case letters. Array identifiers are followed by square brackets ([ ]). An array subscript is required except in an argument or auto list. Arrays are singly dimensioned and can contain up to {BC_DIM_MAX} elements. Indexing begins at zero so an array is indexed from 0 to {BC_DIM_MAX}-1. Subscripts will be truncated to integers. Function identifiers must be followed by parentheses, possibly enclosing arguments. The three types of identifiers do not conflict. The following table summarises the rules for precedence and associativity of all operators. Operators on the same line have the same precedence; rows are in order of decreasing precedence.

OperatorAssociativity++, -- not applicable unary - not applicable ^ right to left *, /, % left to right +, binary - left to right =, +=, -=, *=, /=, %=, ^= right to left ==, <=, >=, !=, <, > none

Table: Operators in bcEach expression or named expression has a

scale, which is the number of decimal digits that are maintained as the fractional portion of the expression.

Named expressionsare places where values are stored. Named expressions are valid on the left side of an assignment. The value of a named expression is the value stored in the place named. Simple identifiers and array elements are named expressions; they have an initial value of zero and an initial scale of zero.The internal registers

scale,ibaseandobaseare all named expressions. The scale of an expression consisting of the name of one of these registers is zero; values assigned to any of these registers will be truncated to integers. Thescaleregister contains a global value used in computing the scale of expressions (as described below). The value of the registerscaleis limited to 0 <=scale<= {BC_SCALE_MAX} and has a default value of zero. Theibaseandobaseregisters are the input and output number radix, respectively. The value ofibaseis limited to:The value of`2 <= ibase <= 16`

obaseis limited to:When either`2 <= obase <= {BC_BASE_MAX}`

ibaseorobaseis assigned a singledigitvalue from the list inLexical Conventions in bc , the value is assumed in hexadecimal. (For example, ibase=A sets to base ten, regardless of the currentibasevalue.) Otherwise, the behaviour is undefined when digits greater than or equal to the value ofibaseappear in the input. Bothibaseandobasehave initial values of 10.Internal computations will be conducted as if in decimal, regardless of the input and output bases, to the specified number of decimal digits. When an exact result is not achieved, (for example, scale=0; 3.2/1) the result will be truncated.

For all values of

obasespecified by this specification, numerical values will be output as follows:

- If the value is less than zero, a hyphen (-) character will be output.

- One of the following will be output, depending on the numerical value:

- If the absolute value of the numerical value is greater than or equal to one, the integer portion of the value will be output as a series of digits appropriate to
obase(as described below). The most significant non-zero digit will be output next, followed by each successively less significant digit.

- If the absolute value of the numerical value is less than one but greater than zero and the scale of the numerical value is greater than zero, it is unspecified whether the character 0 is output.

- If the numerical value is zero, the character 0 will be output.

- If the scale of the value is greater than zero, a period character will be output, followed by a series of digits appropriate to
obase(as described below) representing the most significant portion of the fractional part of the value. Ifsrepresents the scale of the value being output, the number of digits output will besifobaseis 10, less than or equal tosifobaseis greater than 10, or greater than or equal tosifobaseis less than 10. Forobasevalues other than 10, this should be the number of digits needed to represent a precision of 10^{s}.

For

obasevalues from 2 to 16, valid digits are the firstobaseof the single characters:which represent the values zero to 15, inclusive, respectively.`0 1 2 3 4 5 6 7 8 9 A B C D E F`

For bases greater than 16, each digit is written as a separate multi-digit decimal number. Each digit except the most significant fractional digit will be preceded a single space character. For bases from 17 to 100,

bcwill write two-digit decimal numbers; for bases from 101 to 1000, three-digit decimal strings and so on. For example, the decimal number 1024 in base 25 would be written as:in base 125, as:`011524`

`008024`

Very large numbers will be split across lines with 70 characters per line in the POSIX locale; other locales may split at different character boundaries. Lines that are continued must end with a backslash (\).

A function call consists of a function name followed by parentheses containing a comma-separated list of expressions, which are the function arguments. A whole array passed as an argument is specified by the array name followed by empty square brackets. All function arguments are passed by value. As a result, changes made to the formal parameters have no effect on the actual arguments. If the function terminates by executing a

returnstatement, the value of the function will be the value of the expression in the parentheses of thereturnstatement or will be zero if no expression is provided or if there is noreturnstatement.The result of

sqrt(expression) will be the square root of the expression. The result will be truncated in the least significant decimal place. The scale of the result will be the scale of the expression or the value ofscale, whichever is larger.The result of

length(expression) will be the total number of significant decimal digits in the expression. The scale of the result will be zero.The result of

scale(expression) will be the scale of the expression. The scale of the result will be zero.A numeric constant will be an expression. The scale will be the number of digits that follow the radix point in the input representing the constant, or zero if no radix point appears.

The sequence

(expression)will be an expression with the same value and scale asexpression. The parentheses can be used to alter the normal precedence.The semantics of the unary and binary operators are as follows:

- -
expression- The result will be the negative of the
expression. The scale of the result will be the scale ofexpression.The unary increment and decrement operators will not modify the scale of the named expression upon which they operate. The scale of the result will be the scale of that named expression.

- ++
named-expression- The named expression will be incremented by one. The result will be the value of the named expression after incrementing.
- --
named-expression- The named expression will be decremented by one. The result will be the value of the named expression after decrementing.
named-expression++- The named expression will be incremented by one. The result will be the value of the named expression before incrementing.
named-expression--- The named expression will be decremented by one. The result will be the value of the named expression before decrementing.
The exponentiation operator, circumflex (^), binds right to left.

The multiplicative operators ("*", "/", "%") bind left to right.

expression^expression- The result will be the first
expressionraised to the power of the secondexpression. If the second expression is not an integer, the behaviour is undefined. If a is the scale of the left expression and b is the absolute value of the right expression, the scale of the result will be:`if b >= 0 min(a * b, max(scale, a)) if b < 0 scale`

expression*expression- The result will be the product of the two expressions. If a and b are the scales of the two expressions, then the scale of the result will be:
`min(a+b,max(scale,a,b))`

expression/expression- The result will be the quotient of the two expressions. The scale of the result will be the value of
scale.expression%expression- For expressions
aandb, a % b will be evaluated equivalent to the steps:The scale of the result will be:

- Compute a/b to current scale.
- Use the result to compute:
to scale:`a - (a / b) * b`

`max(scale + scale(b), scale(a))`

When`max(scale + scale(b), scale(a))`

scaleis zero, the "%" operator is the mathematical remainder operator.The additive operators ("+", "-") bind left to right.

expression+expression- The result will be the sum of the two expressions. The scale of the result will be the maximum of the scales of the expressions.
expression-expression- The result will be the difference of the two expressions. The scale of the result will be the maximum of the scales of the expressions.
The assignment operators ("=", "+=", "-=", "*=", "/=", "%=", "^=") bind right to left.

named-expression=expression- This expression results in assigning the value of the expression on the right to the named expression on the left. The scale of both the named expression and the result will be the scale of
expression.The compound assignment forms:

are equivalent to:

named-expression <operator>=expressionexcept that the

named-expression=named-expression <operator> expressionnamed-expressionwill be evaluated only once.Unlike all other operators, the relational operators ("<", ">", "<=", ">=", "==", "!=") will be only valid as the object of an

if,whileor inside aforstatement.

expression1<expression2- The relation will be true if the value of
expression1is strictly less than the value ofexpression2.expression1>expression2- The relation will be true if the value of
expression1is strictly greater than the value ofexpression2.expression1<=expression2- The relation will be true if the value of
expression1is less than or equal to the value ofexpression2.expression1>=expression2- The relation will be true if the value of
expression1is greater than or equal to the value ofexpression2.expression1==expression2- The relation will be true if the values of
expression1andexpression2are equal.expression1!=expression2- The relation will be true if the values of
expression1andexpression2are unequal.There are only two storage classes in

bc, global and automatic (local). Only identifiers that are to be local to a function need be declared with theautocommand. The arguments to a function will be local to the function. All other identifiers are assumed to be global and available to all functions. All identifiers, global and local, have initial values of zero. Identifiers declared as auto will be allocated on entry to the function and released on returning from the function. They therefore do not retain values between function calls. Auto arrays will be specified by the array name followed by empty square brackets. On entry to a function, the old values of the names that appear as parameters and as automatic variables are pushed onto a stack. Until the function returns, reference to these names refers only to the new values.References to any of these names from other functions that are called from this function also refer to the new value until one of those functions uses the same name for a local variable.

When a statement is an expression, unless the main operator is an assignment, execution of the statement will write the value of the expression followed by a newline character.

When a statement is a string, execution of the statement will write the value of the string.

Statements separated by semicolons or newline characters will be executed sequentially. In an interactive invocation of

bc, each time a newline character is read that satisfies the grammatical production:the sequential list of statements making up the`input_item : semicolon_list NEWLINE`

semicolon_listwill be executed immediately and any output produced by that execution will be written without any delay due to buffering.In an

ifstatement (if(relation)statement), thestatementwill be executed if the relation is true.The

whilestatement (while(relation)statement) implements a loop in which therelationis tested; each time therelationis true, thestatementwill be executed and therelationretested. When therelationis false, execution will resume afterstatement.A

forstatement (for(expression;relation;expression)statement) is the same as:All three expressions must be present.

first-expressionwhile (relation) {statementlast-expression}The

breakstatement causes termination of afororwhilestatement.The

autostatement (autoidentifier[,identifier]...) will cause the values of the identifiers to be pushed down. The identifiers can be ordinary identifiers or array identifiers. Array identifiers are specified by following the array name by empty square brackets. Theautostatement must be the first statement in a function definition.A

definestatement:defines a function named`define`

LETTER(opt_parameter_list) {opt_auto_define_liststatement_list}LETTER. If a function namedLETTERwas previously defined, thedefinestatement will replace the previous definition. The expression:will invoke the function named

LETTER(opt_argument_list)LETTER. The behaviour is undefined if the number of arguments in the invocation does not match the number of parameters in the definition. Functions will be defined before they are invoked. A function will be considered to be defined within its own body, so recursive calls are valid. The values of numeric constants within a function will be interpreted in the base specified by the value of theibaseregister when the function is invoked.The

returnstatements (returnandreturn(expression)) will cause termination of a function, popping of its auto variables and specifies the result of the function. The first form is equivalent to return(0). The value and scale of an invocation of the function will be the value and scale of the expression in parentheses.The

quitstatement (quit) will stop execution of abcprogram at the point where the statement occurs in the input, even if it occurs in a function definition, or in anif,fororwhilestatement.The following functions will be defined when the

-loption is specified:

s(expression)- Sine of argument in radians.
c(expression)- Cosine of argument in radians.
a(expression)- Arctangent of argument.
l(expression)- Natural logarithm of argument.
e(expression)- Exponential function of argument.
j(expression,expression)- Bessel function of integer order.
The scale of an invocation of each of these functions will be the value of the

scaleregister when the function is invoked. The behaviour is undefined if any of these functions is invoked with an argument outside the domain of the mathematical function.

The following exit values are returned:

- 0
- All input files were processed successfully.
unspecified- An error occurred.

If anyfileoperand is specified and the named file cannot be accessed,bcwill write a diagnostic message to standard error and terminate without any further action.In an interactive invocation of

bc, the utility should print an error message and recover following any error in the input. In a non-interactive invocation ofbc, invalid input causes undefined behaviour.

Automatic variables inbcdo not work in exactly the same way as in either C or PL/1.For historical reasons, the exit status from

bccannot be relied upon to indicate that an error has occurred. Returning zero after an error is possible. Therefore,bcshould be used primarily by interactive users (who can react to error messages) or by application programs that can somehow validate the answers returned as not including error messages.The

bcutility always uses the period (.) character to represent a radix point, regardless of any decimal-point character specified as part of the current locale. In languages like C orawk, the period character is used in program source, so it can be portable and unambiguous, while the locale-specific character is used in input and output. Because there is no distinction between source and input inbc, this arrangement would not be possible. Using the locale-specific character inbc's input would introduce ambiguities into the language; consider the following example in a locale with a comma as the decimal-point character:`define f(a,b) { ... } ... f(1,2,3)`

Because of such ambiguities, the period character is used in input. Having input follow different conventions from output would be confusing in either pipeline usage or interactive usage, so the period is also used in output.

In the shell, the following assigns an approximation of the first ten digits of to the variablex:`x=$(printf "%s\n" 'scale = 10; 104348/33215' | bc)`

The following

bcprogram prints the same approximation of , with a label, to standard output:`scale = 10 "pi equals " 104348 / 33215`

The following defines a function to compute an approximate value of the exponential function (note that such a function is predefined if the

-loption is specified):`scale = 20 define e(x){ auto a, b, c, i, s a = 1 b = 1 s = 1 for (i = 1; 1 == 1; i++){ a = a*x b = b*i c = a/b if (c == 0) { return(s) } s = s+c } }`

The following prints approximate values of the exponential function of the first ten integers:

`for (i = 1; i <= 10; ++i) { e(i) }`

The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns about parts of this interface definition to the IEEE PASC Shell and Utilities Working Group which is identifying the corrections. A future revision of this specification will align with IEEE Std. 1003.2b when finalised.

awk.

Copyright © 1997 The Open Group

[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]