1271 lines
46 KiB
HTML
1271 lines
46 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>bc</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="bc"></a> <a name="tag_04_09"></a><!-- bc -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_04_09_01"></a>NAME</h4>
|
|
|
|
<blockquote>bc - arbitrary-precision arithmetic language</blockquote>
|
|
|
|
<h4><a name="tag_04_09_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>bc</tt> <b>[</b><tt>-l</tt><b>] [</b><i>file</i> <tt>...</tt><b>]</b></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>bc</i> utility shall implement an arbitrary precision calculator. It shall take input from any files given, then read
|
|
from the standard input. If the standard input and standard output to <i>bc</i> are attached to a terminal, the invocation of
|
|
<i>bc</i> shall be considered to be <i>interactive</i>, causing behavioral constraints described in the following sections.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_04"></a>OPTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>bc</i> utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
|
"../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p>
|
|
|
|
<p>The following option shall be supported:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>-l</b></dt>
|
|
|
|
<dd>(The letter ell.) Define the math functions and initialize <i>scale</i> to 20, instead of the default zero; see the EXTENDED
|
|
DESCRIPTION section.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_05"></a>OPERANDS</h4>
|
|
|
|
<blockquote>
|
|
<p>The following operand shall be supported:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>file</i></dt>
|
|
|
|
<dd>A pathname of a text file containing <i>bc</i> program statements. After all <i>file</i>s have been read, <i>bc</i> shall read
|
|
the standard input.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_06"></a>STDIN</h4>
|
|
|
|
<blockquote>
|
|
<p>See the INPUT FILES section.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_07"></a>INPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>Input files shall be text files containing a sequence of comments, statements, and function definitions that shall be executed
|
|
as they are read.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_08"></a>ENVIRONMENT VARIABLES</h4>
|
|
|
|
<blockquote>
|
|
<p>The following environment variables shall affect the execution of <i>bc</i>:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>LANG</i></dt>
|
|
|
|
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
|
|
IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
|
|
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
|
|
|
|
<dt><i>LC_ALL</i></dt>
|
|
|
|
<dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
|
|
|
|
<dt><i>LC_CTYPE</i></dt>
|
|
|
|
<dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as
|
|
opposed to multi-byte characters in arguments and input files).</dd>
|
|
|
|
<dt><i>LC_MESSAGES</i></dt>
|
|
|
|
<dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard
|
|
error.</dd>
|
|
|
|
<dt><i>NLSPATH</i></dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
|
|
"[Option End]" border="0"></dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_09"></a>ASYNCHRONOUS EVENTS</h4>
|
|
|
|
<blockquote>
|
|
<p>Default.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_10"></a>STDOUT</h4>
|
|
|
|
<blockquote>
|
|
<p>The output of the <i>bc</i> utility shall be controlled by the program read, and consist of zero or more lines containing the
|
|
value of all executed expressions without assignments. The radix and precision of the output shall be controlled by the values of
|
|
the <b>obase</b> and <b>scale</b> variables; see the EXTENDED DESCRIPTION section.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_11"></a>STDERR</h4>
|
|
|
|
<blockquote>
|
|
<p>The standard error shall be used only for diagnostic messages.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_12"></a>OUTPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_13"></a>EXTENDED DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<h5><a name="tag_04_09_13_01"></a>Grammar</h5>
|
|
|
|
<p>The grammar in this section and the lexical conventions in the following section shall together describe the syntax for
|
|
<i>bc</i> programs. The general conventions for this style of grammar are described in <a href=
|
|
"xcu_chap01.html#tag_01_10"><i>Grammar Conventions</i></a> . A valid program can be represented as the non-terminal symbol
|
|
<b>program</b> in the grammar. This formal syntax shall take precedence over the text syntax description.</p>
|
|
|
|
<pre>
|
|
<tt>%token EOF NEWLINE STRING LETTER NUMBER
|
|
<br>
|
|
%token MUL_OP
|
|
/* '*', '/', '%' */
|
|
<br>
|
|
%token ASSIGN_OP
|
|
/* '=', '+=', '-=', '*=', '/=', '%=', '^=' */
|
|
<br>
|
|
%token REL_OP
|
|
/* '==', '<=', '>=', '!=', '<', '>' */
|
|
<br>
|
|
%token INCR_DECR
|
|
/* '++', '--' */
|
|
<br>
|
|
%token Define Break Quit Length
|
|
/* 'define', 'break', 'quit', 'length' */
|
|
<br>
|
|
%token Return For If While Sqrt
|
|
/* 'return', 'for', 'if', 'while', 'sqrt' */
|
|
<br>
|
|
%token Scale Ibase Obase Auto
|
|
/* 'scale', 'ibase', 'obase', 'auto' */
|
|
<br>
|
|
%start program
|
|
<br>
|
|
%%
|
|
<br>
|
|
program : EOF
|
|
| input_item program
|
|
;
|
|
<br>
|
|
input_item : semicolon_list NEWLINE
|
|
| function
|
|
;
|
|
<br>
|
|
semicolon_list : /* empty */
|
|
| statement
|
|
| semicolon_list ';' statement
|
|
| semicolon_list ';'
|
|
;
|
|
<br>
|
|
statement_list : /* empty */
|
|
| statement
|
|
| statement_list NEWLINE
|
|
| statement_list NEWLINE statement
|
|
| statement_list ';'
|
|
| statement_list ';' statement
|
|
;
|
|
<br>
|
|
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 '}'
|
|
;
|
|
<br>
|
|
function : Define LETTER '(' opt_parameter_list ')'
|
|
'{' NEWLINE opt_auto_define_list
|
|
statement_list '}'
|
|
;
|
|
<br>
|
|
opt_parameter_list : /* empty */
|
|
| parameter_list
|
|
;
|
|
<br>
|
|
parameter_list : LETTER
|
|
| define_list ',' LETTER
|
|
;
|
|
<br>
|
|
opt_auto_define_list : /* empty */
|
|
| Auto define_list NEWLINE
|
|
| Auto define_list ';'
|
|
;
|
|
<br>
|
|
define_list : LETTER
|
|
| LETTER '[' ']'
|
|
| define_list ',' LETTER
|
|
| define_list ',' LETTER '[' ']'
|
|
;
|
|
<br>
|
|
opt_argument_list : /* empty */
|
|
| argument_list
|
|
;
|
|
<br>
|
|
argument_list : expression
|
|
| LETTER '[' ']' ',' argument_list
|
|
;
|
|
<br>
|
|
relational_expression : expression
|
|
| expression REL_OP expression
|
|
;
|
|
<br>
|
|
return_expression : /* empty */
|
|
| expression
|
|
;
|
|
<br>
|
|
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 ')'
|
|
;
|
|
<br>
|
|
named_expression : LETTER
|
|
| LETTER '[' expression ']'
|
|
| Scale
|
|
| Ibase
|
|
| Obase
|
|
;
|
|
</tt>
|
|
</pre>
|
|
|
|
<h5><a name="tag_04_09_13_02"></a>Lexical Conventions in bc</h5>
|
|
|
|
<p>The lexical conventions for <i>bc</i> programs, with respect to the preceding grammar, shall be as follows:</p>
|
|
|
|
<ol>
|
|
<li>
|
|
<p>Except as noted, <i>bc</i> shall recognize the longest possible token or delimiter beginning at a given point.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>A comment shall consist of any characters beginning with the two adjacent characters <tt>"/*"</tt> and terminated by the next
|
|
occurrence of the two adjacent characters <tt>"*/"</tt> . Comments shall have no effect except to delimit lexical tokens.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The <newline> shall be recognized as the token <b>NEWLINE</b>.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The token <b>STRING</b> shall represent a string constant; it shall consist of any characters beginning with the double-quote
|
|
character ( <tt>' )'</tt> 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 shall be 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
|
|
shall be limited to {BC_STRING_MAX} bytes.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>A <blank> shall have no effect except as an ordinary character if it appears within a <b>STRING</b> token, or to delimit a
|
|
lexical token other than <b>STRING</b>.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The combination of a backslash character immediately followed by a <newline> shall have no effect other than to delimit
|
|
lexical tokens with the following exceptions:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>It shall be interpreted as the character sequence <tt>"\<newline>"</tt> in <b>STRING</b> tokens.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>It shall be ignored as part of a multi-line <b>NUMBER</b> token.</p>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The token <b>NUMBER</b> shall represent a numeric constant. It shall be recognized by the following grammar:</p>
|
|
|
|
<pre>
|
|
<tt>NUMBER : integer
|
|
| '.' integer
|
|
| integer '.'
|
|
| integer '.' integer
|
|
;
|
|
<br>
|
|
integer : digit
|
|
| integer digit
|
|
;
|
|
<br>
|
|
digit : 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
|
|
| 8 | 9 | A | B | C | D | E | F
|
|
;
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The value of a <b>NUMBER</b> token shall be interpreted as a numeral in the base specified by the value of the internal register
|
|
<b>ibase</b> (described below). Each of the <b>digit</b> characters shall have the value from 0 to 15 in the order listed here, and
|
|
the period character shall represent the radix point. The behavior is undefined if digits greater than or equal to the value of
|
|
<b>ibase</b> appear in the token. However, note the exception for single-digit values being assigned to <b>ibase</b> and
|
|
<b>obase</b> themselves, in <a href="#tag_04_09_13_03">Operations in bc</a> .</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The following keywords shall be recognized as tokens:</p>
|
|
|
|
<blockquote>
|
|
<table cellpadding="3">
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><b><br>
|
|
auto<br>
|
|
break<br>
|
|
define<br>
|
|
</b></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent"><b><br>
|
|
ibase<br>
|
|
if<br>
|
|
for<br>
|
|
</b></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent"><b><br>
|
|
length<br>
|
|
obase<br>
|
|
quit<br>
|
|
</b></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent"><b><br>
|
|
return<br>
|
|
scale<br>
|
|
sqrt<br>
|
|
</b></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent"><b><br>
|
|
while<br>
|
|
</b></p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</blockquote>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Any of the following characters occurring anywhere except within a keyword shall be recognized as the token <b>LETTER</b>:</p>
|
|
|
|
<pre>
|
|
<tt>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
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The following single-character and two-character sequences shall be recognized as the token <b>ASSIGN_OP</b>:</p>
|
|
|
|
<pre>
|
|
<tt>= += -= *= /= %= ^=
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If an <tt>'='</tt> character, as the beginning of a token, is followed by a <tt>'-'</tt> character with no intervening
|
|
delimiter, the behavior is undefined.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The following single-characters shall be recognized as the token <b>MUL_OP</b>:</p>
|
|
|
|
<pre>
|
|
<tt>* / %
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The following single-character and two-character sequences shall be recognized as the token <b>REL_OP</b>:</p>
|
|
|
|
<pre>
|
|
<tt>== <= >= != < >
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The following two-character sequences shall be recognized as the token <b>INCR_DECR</b>:</p>
|
|
|
|
<pre>
|
|
<tt>++ --
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The following single characters shall be recognized as tokens whose names are the character:</p>
|
|
|
|
<pre>
|
|
<tt><newline> ( ) , + - ; [ ] ^ { }
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The token <b>EOF</b> is returned when the end of input is reached.</p>
|
|
</li>
|
|
</ol>
|
|
|
|
<h5><a name="tag_04_09_13_03"></a>Operations in bc</h5>
|
|
|
|
<p>There are three kinds of identifiers: ordinary identifiers, array identifiers, and function identifiers. All three types consist
|
|
of single lowercase letters. Array identifiers shall be followed by square brackets ( <tt>"[]"</tt> ). 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
|
|
shall begin at zero so an array is indexed from 0 to {BC_DIM_MAX}-1. Subscripts shall be truncated to integers. The application
|
|
shall ensure that function identifiers are followed by parentheses, possibly enclosing arguments. The three types of identifiers do
|
|
not conflict.</p>
|
|
|
|
<p>The following table summarizes the rules for precedence and associativity of all operators. Operators on the same line shall
|
|
have the same precedence; rows are in order of decreasing precedence.</p>
|
|
|
|
<center><b>Table: Operators in <i>bc</i></b></center>
|
|
|
|
<center>
|
|
<table border="1" cellpadding="3" align="center">
|
|
<tr valign="top">
|
|
<th align="center">
|
|
<p class="tent"><b>Operator</b></p>
|
|
</th>
|
|
<th align="center">
|
|
<p class="tent"><b>Associativity</b></p>
|
|
</th>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">++, --</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">N/A</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">unary -</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">N/A</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">^</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Right to left</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">*, /, %</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Left to right</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">+, binary -</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Left to right</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">=, +=, -=, *=, /=, %=, ^=</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Right to left</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">==, <=, >=, !=, <, ></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">None</p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
|
|
<p>Each expression or named expression has a <i>scale</i>, which is the number of decimal digits that shall be maintained as the
|
|
fractional portion of the expression.</p>
|
|
|
|
<p><i>Named expressions</i> are places where values are stored. Named expressions shall be valid on the left side of an assignment.
|
|
The value of a named expression shall be 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.</p>
|
|
|
|
<p>The internal registers <b>scale</b>, <b>ibase</b>, and <b>obase</b> are all named expressions. The scale of an expression
|
|
consisting of the name of one of these registers shall be zero; values assigned to any of these registers are truncated to
|
|
integers. The <b>scale</b> register shall contain a global value used in computing the scale of expressions (as described below).
|
|
The value of the register <b>scale</b> is limited to 0 <= <b>scale</b> <= {BC_SCALE_MAX} and shall have a default value of
|
|
zero. The <b>ibase</b> and <b>obase</b> registers are the input and output number radix, respectively. The value of <b>ibase</b>
|
|
shall be limited to:</p>
|
|
|
|
<pre>
|
|
<tt>2 <= ibase <= 16
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The value of <b>obase</b> shall be limited to:</p>
|
|
|
|
<pre>
|
|
<tt>2 <= obase <= {BC_BASE_MAX}
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>When either <b>ibase</b> or <b>obase</b> is assigned a single <b>digit</b> value from the list in <a href=
|
|
"#tag_04_09_13_02">Lexical Conventions in bc</a> , the value shall be assumed in hexadecimal. (For example, <b>ibase</b>=A sets to
|
|
base ten, regardless of the current <b>ibase</b> value.) Otherwise, the behavior is undefined when digits greater than or equal to
|
|
the value of <b>ibase</b> appear in the input. Both <b>ibase</b> and <b>obase</b> shall have initial values of 10.</p>
|
|
|
|
<p>Internal computations shall 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, <b>scale</b>=0; 3.2/1)<b>,</b> the result shall be
|
|
truncated.</p>
|
|
|
|
<p>For all values of <b>obase</b> specified by this volume of IEEE Std 1003.1-2001, <i>bc</i> shall output numeric values
|
|
by performing each of the following steps in order:</p>
|
|
|
|
<ol>
|
|
<li>
|
|
<p>If the value is less than zero, a hyphen ( <tt>'-'</tt> ) character shall be output.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>One of the following is output, depending on the numerical value:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>If the absolute value of the numerical value is greater than or equal to one, the integer portion of the value shall be output
|
|
as a series of digits appropriate to <b>obase</b> (as described below), most significant digit first. The most significant non-zero
|
|
digit shall be output next, followed by each successively less significant digit.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>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.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If the numerical value is zero, the character 0 shall be output.</p>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If the scale of the value is greater than zero and the numeric value is not zero, a period character shall be output, followed
|
|
by a series of digits appropriate to <b>obase</b> (as described below) representing the most significant portion of the fractional
|
|
part of the value. If <i>s</i> represents the scale of the value being output, the number of digits output shall be <i>s</i> if
|
|
<b>obase</b> is 10, less than or equal to <i>s</i> if <b>obase</b> is greater than 10, or greater than or equal to <i>s</i> if
|
|
<b>obase</b> is less than 10. For <b>obase</b> values other than 10, this should be the number of digits needed to represent a
|
|
precision of 10<sup><small><i>s</i></small></sup>.</p>
|
|
</li>
|
|
</ol>
|
|
|
|
<p>For <b>obase</b> values from 2 to 16, valid digits are the first <b>obase</b> of the single characters:</p>
|
|
|
|
<pre>
|
|
<tt>0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>which represent the values zero to 15, inclusive, respectively.</p>
|
|
|
|
<p>For bases greater than 16, each digit shall be written as a separate multi-digit decimal number. Each digit except the most
|
|
significant fractional digit shall be preceded by a single <space>. For bases from 17 to 100, <i>bc</i> shall 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:</p>
|
|
|
|
<pre>
|
|
<tt><img src="../images/delta.gif" border="0">01<img src="../images/delta.gif" border="0">15<img src="../images/delta.gif" border=
|
|
"0">24
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>and in base 125, as:</p>
|
|
|
|
<pre>
|
|
<tt><img src="../images/delta.gif" border="0">008<img src="../images/delta.gif" border="0">024
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>Very large numbers shall 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 shall end with a backslash ( <tt>'\'</tt> ).</p>
|
|
|
|
<p>A function call shall consist 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 shall be specified by the array name followed by empty square
|
|
brackets. All function arguments shall be passed by value. As a result, changes made to the formal parameters shall have no effect
|
|
on the actual arguments. If the function terminates by executing a <b>return</b> statement, the value of the function shall be the
|
|
value of the expression in the parentheses of the <b>return</b> statement or shall be zero if no expression is provided or if there
|
|
is no <b>return</b> statement.</p>
|
|
|
|
<p>The result of <b>sqrt</b>( <i>expression</i>) shall be the square root of the expression. The result shall be truncated in the
|
|
least significant decimal place. The scale of the result shall be the scale of the expression or the value of <b>scale</b>,
|
|
whichever is larger.</p>
|
|
|
|
<p>The result of <b>length</b>( <i>expression</i>) shall be the total number of significant decimal digits in the expression. The
|
|
scale of the result shall be zero.</p>
|
|
|
|
<p>The result of <b>scale</b>( <i>expression</i>) shall be the scale of the expression. The scale of the result shall be zero.</p>
|
|
|
|
<p>A numeric constant shall be an expression. The scale shall be the number of digits that follow the radix point in the input
|
|
representing the constant, or zero if no radix point appears.</p>
|
|
|
|
<p>The sequence ( <i>expression</i> ) shall be an expression with the same value and scale as <i>expression</i>. The
|
|
parentheses can be used to alter the normal precedence.</p>
|
|
|
|
<p>The semantics of the unary and binary operators are as follows:</p>
|
|
|
|
<dl compact>
|
|
<dt>-<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
The result shall be the negative of the <i>expression</i>. The scale of the result shall be the scale of <i>expression</i>.</dd>
|
|
</dl>
|
|
|
|
<p>The unary increment and decrement operators shall not modify the scale of the named expression upon which they operate. The
|
|
scale of the result shall be the scale of that named expression.</p>
|
|
|
|
<dl compact>
|
|
<dt>++<i>named-expression</i></dt>
|
|
|
|
<dd><br>
|
|
The named expression shall be incremented by one. The result shall be the value of the named expression after incrementing.</dd>
|
|
|
|
<dt>--<i>named-expression</i></dt>
|
|
|
|
<dd><br>
|
|
The named expression shall be decremented by one. The result shall be the value of the named expression after decrementing.</dd>
|
|
|
|
<dt><i>named-expression</i>++</dt>
|
|
|
|
<dd><br>
|
|
The named expression shall be incremented by one. The result shall be the value of the named expression before incrementing.</dd>
|
|
|
|
<dt><i>named-expression</i>--</dt>
|
|
|
|
<dd><br>
|
|
The named expression shall be decremented by one. The result shall be the value of the named expression before decrementing.</dd>
|
|
</dl>
|
|
|
|
<p>The exponentiation operator, circumflex ( <tt>'^'</tt> ), shall bind right to left.</p>
|
|
|
|
<dl compact>
|
|
<dt><i>expression</i>^<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
The result shall be the first <i>expression</i> raised to the power of the second <i>expression</i>. If the second expression is
|
|
not an integer, the behavior is undefined. If <i>a</i> is the scale of the left expression and <i>b</i> is the absolute value of
|
|
the right expression, the scale of the result shall be:
|
|
|
|
<pre>
|
|
<tt>if b >= 0 min(a * b, max(scale, a)) if b < 0 scale
|
|
</tt>
|
|
</pre>
|
|
</dd>
|
|
</dl>
|
|
|
|
The multiplicative operators ( <tt>'*'</tt> , <tt>'/'</tt> , <tt>'%'</tt> ) shall bind left to right.
|
|
|
|
<dl compact>
|
|
<dt><i>expression</i>*<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
The result shall be the product of the two expressions. If <i>a</i> and <i>b</i> are the scales of the two expressions, then the
|
|
scale of the result shall be:
|
|
|
|
<pre>
|
|
<tt>min(a+b,max(scale,a,b))
|
|
</tt>
|
|
</pre>
|
|
</dd>
|
|
|
|
<dt><i>expression</i>/<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
The result shall be the quotient of the two expressions. The scale of the result shall be the value of <b>scale</b>.</dd>
|
|
|
|
<dt><i>expression</i>%<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
For expressions <i>a</i> and <i>b</i>, <i>a</i>% <i>b</i> shall be evaluated equivalent to the steps:
|
|
|
|
<ol>
|
|
<li>
|
|
<p>Compute <i>a</i>/ <i>b</i> to current scale.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Use the result to compute:</p>
|
|
|
|
<pre>
|
|
<tt>a - (a / b) * b
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>to scale:</p>
|
|
|
|
<pre>
|
|
<tt>max(scale + scale(b), scale(a))
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
</ol>
|
|
|
|
The scale of the result shall be:
|
|
|
|
<pre>
|
|
<tt>max(scale + scale(b), scale(a))
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>When <b>scale</b> is zero, the <tt>'%'</tt> operator is the mathematical remainder operator.</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
<p>The additive operators ( <tt>'+'</tt> , <tt>'-'</tt> ) shall bind left to right.</p>
|
|
|
|
<dl compact>
|
|
<dt><i>expression</i>+<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
The result shall be the sum of the two expressions. The scale of the result shall be the maximum of the scales of the
|
|
expressions.</dd>
|
|
|
|
<dt><i>expression</i>-<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
The result shall be the difference of the two expressions. The scale of the result shall be the maximum of the scales of the
|
|
expressions.</dd>
|
|
</dl>
|
|
|
|
<p>The assignment operators ( <tt>'='</tt> , <tt>"+="</tt> , <tt>"-="</tt> , <tt>"*="</tt> , <tt>"/="</tt> , <tt>"%="</tt> ,
|
|
<tt>"^="</tt> ) shall bind right to left.</p>
|
|
|
|
<dl compact>
|
|
<dt><i>named-expression</i>=<i>expression</i></dt>
|
|
|
|
<dd><br>
|
|
This expression shall result 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 shall be the scale of <i>expression</i>.</dd>
|
|
</dl>
|
|
|
|
<p>The compound assignment forms:</p>
|
|
|
|
<pre>
|
|
<i>named-expression</i> <tt><</tt><i>operator</i><tt>>=</tt> <i>expression</i>
|
|
</pre>
|
|
|
|
<p>shall be equivalent to:</p>
|
|
|
|
<pre>
|
|
<i>named-expression</i><tt>=</tt><i>named-expression</i> <tt><</tt><i>operator</i><tt>></tt> <i>expression</i>
|
|
</pre>
|
|
|
|
<p>except that the <i>named-expression</i> shall be evaluated only once.</p>
|
|
|
|
<p>Unlike all other operators, the relational operators ( <tt>'<'</tt> , <tt>'>'</tt> , <tt>"<="</tt> , <tt>">="</tt> ,
|
|
<tt>"=="</tt> , <tt>"!="</tt> ) shall be only valid as the object of an <b>if</b>, <b>while</b>, or inside a <b>for</b>
|
|
statement.</p>
|
|
|
|
<dl compact>
|
|
<dt><i>expression1</i><<i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
The relation shall be true if the value of <i>expression1</i> is strictly less than the value of <i>expression2</i>.</dd>
|
|
|
|
<dt><i>expression1</i>><i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
The relation shall be true if the value of <i>expression1</i> is strictly greater than the value of <i>expression2</i>.</dd>
|
|
|
|
<dt><i>expression1</i><=<i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
The relation shall be true if the value of <i>expression1</i> is less than or equal to the value of <i>expression2</i>.</dd>
|
|
|
|
<dt><i>expression1</i>>=<i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
The relation shall be true if the value of <i>expression1</i> is greater than or equal to the value of <i>expression2</i>.</dd>
|
|
|
|
<dt><i>expression1</i>==<i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
The relation shall be true if the values of <i>expression1</i> and <i>expression2</i> are equal.</dd>
|
|
|
|
<dt><i>expression1</i>!=<i>expression2</i></dt>
|
|
|
|
<dd><br>
|
|
The relation shall be true if the values of <i>expression1</i> and <i>expression2</i> are unequal.</dd>
|
|
</dl>
|
|
|
|
<p>There are only two storage classes in <i>bc</i>: global and automatic (local). Only identifiers that are local to a function
|
|
need be declared with the <b>auto</b> command. The arguments to a function shall 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 shall 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 shall 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 shall be pushed
|
|
onto a stack. Until the function returns, reference to these names shall refer only to the new values.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
<p>When a statement is an expression, unless the main operator is an assignment, execution of the statement shall write the value
|
|
of the expression followed by a <newline>.</p>
|
|
|
|
<p>When a statement is a string, execution of the statement shall write the value of the string.</p>
|
|
|
|
<p>Statements separated by semicolons or <newline>s shall be executed sequentially. In an interactive invocation of
|
|
<i>bc</i>, each time a <newline> is read that satisfies the grammatical production:</p>
|
|
|
|
<pre>
|
|
<tt>input_item : semicolon_list NEWLINE
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>the sequential list of statements making up the <b>semicolon_list</b> shall be executed immediately and any output produced by
|
|
that execution shall be written without any delay due to buffering.</p>
|
|
|
|
<p>In an <b>if</b> statement ( <b>if</b>( <i>relation</i>) <i>statement</i>), the <i>statement</i> shall be executed if the
|
|
relation is true.</p>
|
|
|
|
<p>The <b>while</b> statement ( <b>while</b>( <i>relation</i>) <i>statement</i>) implements a loop in which the <i>relation</i> is
|
|
tested; each time the <i>relation</i> is true, the <i>statement</i> shall be executed and the <i>relation</i> retested. When the
|
|
<i>relation</i> is false, execution shall resume after <i>statement</i>.</p>
|
|
|
|
<p>A <b>for</b> statement( <b>for</b>( <i>expression</i>; <i>relation</i>; <i>expression</i>) <i>statement</i>) shall be the same
|
|
as:</p>
|
|
|
|
<pre>
|
|
<i>first-expression</i><tt>while (</tt><i>relation</i><tt>) {
|
|
</tt> <i>statement</i> <tt> </tt> <i>last-expression</i><tt>}
|
|
</tt>
|
|
</pre>
|
|
|
|
The application shall ensure that all three expressions are present.
|
|
|
|
<p>The <b>break</b> statement shall cause termination of a <b>for</b> or <b>while</b> statement.</p>
|
|
|
|
<p>The <b>auto</b> statement ( <b>auto</b> <i>identifier</i> <b>[</b>, <i>identifier</i> <b>]</b> ...) shall cause the values of
|
|
the identifiers to be pushed down. The identifiers can be ordinary identifiers or array identifiers. Array identifiers shall be
|
|
specified by following the array name by empty square brackets. The application shall ensure that the <b>auto</b> statement is the
|
|
first statement in a function definition.</p>
|
|
|
|
<p>A <b>define</b> statement:</p>
|
|
|
|
<pre>
|
|
<tt>define</tt> <i>LETTER</i> <tt>(</tt> <i>opt_parameter_list</i> <tt>) {
|
|
</tt> <i>opt_auto_define_list</i> <tt> </tt> <i>statement_list</i><tt>}
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>defines a function named <b>LETTER</b>. If a function named <b>LETTER</b> was previously defined, the <b>define</b> statement
|
|
shall replace the previous definition. The expression:</p>
|
|
|
|
<pre>
|
|
<tt>LETTER (</tt> <i>opt_argument_list</i> <tt>)
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>shall invoke the function named <b>LETTER</b>. The behavior is undefined if the number of arguments in the invocation does not
|
|
match the number of parameters in the definition. Functions shall be defined before they are invoked. A function shall be
|
|
considered to be defined within its own body, so recursive calls are valid. The values of numeric constants within a function shall
|
|
be interpreted in the base specified by the value of the <b>ibase</b> register when the function is invoked.</p>
|
|
|
|
<p>The <b>return</b> statements ( <b>return</b> and <b>return</b>( <i>expression</i>)) shall cause termination of a function,
|
|
popping of its auto variables, and specification of the result of the function. The first form shall be equivalent to
|
|
<b>return</b>(0). The value and scale of the result returned by the function shall be the value and scale of the expression
|
|
returned.</p>
|
|
|
|
<p>The <b>quit</b> statement ( <b>quit</b>) shall stop execution of a <i>bc</i> program at the point where the statement occurs in
|
|
the input, even if it occurs in a function definition, or in an <b>if</b>, <b>for</b>, or <b>while</b> statement.</p>
|
|
|
|
<p>The following functions shall be defined when the <b>-l</b> option is specified:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>s</b>( <i>expression</i> )</dt>
|
|
|
|
<dd><br>
|
|
Sine of argument in radians.</dd>
|
|
|
|
<dt><b>c</b>( <i>expression</i> )</dt>
|
|
|
|
<dd><br>
|
|
Cosine of argument in radians.</dd>
|
|
|
|
<dt><b>a</b>( <i>expression</i> )</dt>
|
|
|
|
<dd><br>
|
|
Arctangent of argument.</dd>
|
|
|
|
<dt><b>l</b>( <i>expression</i> )</dt>
|
|
|
|
<dd><br>
|
|
Natural logarithm of argument.</dd>
|
|
|
|
<dt><b>e</b>( <i>expression</i> )</dt>
|
|
|
|
<dd><br>
|
|
Exponential function of argument.</dd>
|
|
|
|
<dt><b>j</b>( <i>expression</i>, <i>expression</i> )</dt>
|
|
|
|
<dd><br>
|
|
Bessel function of integer order.</dd>
|
|
</dl>
|
|
|
|
<p>The scale of the result returned by these functions shall be the value of the <b>scale</b> register at the time the function is
|
|
invoked. The value of the <b>scale</b> register after these functions have completed their execution shall be the same value it had
|
|
upon invocation. The behavior is undefined if any of these functions is invoked with an argument outside the domain of the
|
|
mathematical function.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_14"></a>EXIT STATUS</h4>
|
|
|
|
<blockquote>
|
|
<p>The following exit values shall be returned:</p>
|
|
|
|
<dl compact>
|
|
<dt>0</dt>
|
|
|
|
<dd>All input files were processed successfully.</dd>
|
|
|
|
<dt><i>unspecified</i></dt>
|
|
|
|
<dd>An error occurred.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_15"></a>CONSEQUENCES OF ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>If any <i>file</i> operand is specified and the named file cannot be accessed, <i>bc</i> shall write a diagnostic message to
|
|
standard error and terminate without any further action.</p>
|
|
|
|
<p>In an interactive invocation of <i>bc</i>, the utility should print an error message and recover following any error in the
|
|
input. In a non-interactive invocation of <i>bc</i>, invalid input causes undefined behavior.</p>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_04_09_16"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>Automatic variables in <i>bc</i> do not work in exactly the same way as in either C or PL/1.</p>
|
|
|
|
<p>For historical reasons, the exit status from <i>bc</i> cannot be relied upon to indicate that an error has occurred. Returning
|
|
zero after an error is possible. Therefore, <i>bc</i> should 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.</p>
|
|
|
|
<p>The <i>bc</i> utility always uses the period ( <tt>'.'</tt> ) character to represent a radix point, regardless of any
|
|
decimal-point character specified as part of the current locale. In languages like C or <a href=
|
|
"../utilities/awk.html"><i>awk</i></a>, 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 in
|
|
<i>bc</i>, this arrangement would not be possible. Using the locale-specific character in <i>bc</i>'s input would introduce
|
|
ambiguities into the language; consider the following example in a locale with a comma as the decimal-point character:</p>
|
|
|
|
<pre>
|
|
<tt>define f(a,b) {
|
|
...
|
|
}
|
|
...
|
|
<br>
|
|
f(1,2,3)
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>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.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_17"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<p>In the shell, the following assigns an approximation of the first ten digits of <tt>'<img src="../images/pi.gif" border=
|
|
"0">'</tt> to the variable <i>x</i>:</p>
|
|
|
|
<pre>
|
|
<tt>x=$(printf "%s\n" 'scale = 10; 104348/33215' | bc)
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The following <i>bc</i> program prints the same approximation of <tt>'<img src="../images/pi.gif" border="0">'</tt> , with a
|
|
label, to standard output:</p>
|
|
|
|
<pre>
|
|
<tt>scale = 10
|
|
"pi equals "
|
|
104348 / 33215
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The following defines a function to compute an approximate value of the exponential function (note that such a function is
|
|
predefined if the <b>-l</b> option is specified):</p>
|
|
|
|
<pre>
|
|
<tt>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
|
|
}
|
|
}
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The following prints approximate values of the exponential function of the first ten integers:</p>
|
|
|
|
<pre>
|
|
<tt>for (i = 1; i <= 10; ++i) {
|
|
e(i)
|
|
}
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_18"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>bc</i> utility is implemented historically as a front-end processor for <i>dc</i>; <i>dc</i> was not selected to be part
|
|
of this volume of IEEE Std 1003.1-2001 because <i>bc</i> was thought to have a more intuitive programmatic interface.
|
|
Current implementations that implement <i>bc</i> using <i>dc</i> are expected to be compliant.</p>
|
|
|
|
<p>The exit status for error conditions has been left unspecified for several reasons:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>The <i>bc</i> utility is used in both interactive and non-interactive situations. Different exit codes may be appropriate for
|
|
the two uses.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>It is unclear when a non-zero exit should be given; divide-by-zero, undefined functions, and syntax errors are all
|
|
possibilities.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>It is not clear what utility the exit status has.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>In the 4.3 BSD, System V, and Ninth Edition implementations, <i>bc</i> works in conjunction with <i>dc</i>. The <i>dc</i>
|
|
utility is the parent, <i>bc</i> is the child. This was done to cleanly terminate <i>bc</i> if <i>dc</i> aborted.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>The decision to have <i>bc</i> exit upon encountering an inaccessible input file is based on the belief that <i>bc</i>
|
|
<i>file1</i> <i>file2</i> is used most often when at least <i>file1</i> contains data/function declarations/initializations. Having
|
|
<i>bc</i> continue with prerequisite files missing is probably not useful. There is no implication in the CONSEQUENCES OF ERRORS
|
|
section that <i>bc</i> must check all its files for accessibility before opening any of them.</p>
|
|
|
|
<p>There was considerable debate on the appropriateness of the language accepted by <i>bc</i>. Several reviewers preferred to see
|
|
either a pure subset of the C language or some changes to make the language more compatible with C. While the <i>bc</i> language
|
|
has some obvious similarities to C, it has never claimed to be compatible with any version of C. An interpreter for a subset of C
|
|
might be a very worthwhile utility, and it could potentially make <i>bc</i> obsolete. However, no such utility is known in
|
|
historical practice, and it was not within the scope of this volume of IEEE Std 1003.1-2001 to define such a language and
|
|
utility. If and when they are defined, it may be appropriate to include them in a future version of IEEE Std 1003.1. This
|
|
left the following alternatives:</p>
|
|
|
|
<ol>
|
|
<li>
|
|
<p>Exclude any calculator language from this volume of IEEE Std 1003.1-2001.</p>
|
|
|
|
<p>The consensus of the standard developers was that a simple programmatic calculator language is very useful for both applications
|
|
and interactive users. The only arguments for excluding any calculator were that it would become obsolete if and when a
|
|
C-compatible one emerged, or that the absence would encourage the development of such a C-compatible one. These arguments did not
|
|
sufficiently address the needs of current application writers.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Standardize the historical <i>dc</i>, possibly with minor modifications.</p>
|
|
|
|
<p>The consensus of the standard developers was that <i>dc</i> is a fundamentally less usable language and that that would be far
|
|
too severe a penalty for avoiding the issue of being similar to but incompatible with C.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Standardize the historical <i>bc</i>, possibly with minor modifications.</p>
|
|
|
|
<p>This was the approach taken. Most of the proponents of changing the language would not have been satisfied until most or all of
|
|
the incompatibilities with C were resolved. Since most of the changes considered most desirable would break historical applications
|
|
and require significant modification to historical implementations, almost no modifications were made. The one significant
|
|
modification that was made was the replacement of the historical <i>bc</i> assignment operators <tt>"=+"</tt> , and so on, with the
|
|
more modern <tt>"+="</tt> , and so on. The older versions are considered to be fundamentally flawed because of the lexical
|
|
ambiguity in uses like <i>a</i>=-1.</p>
|
|
|
|
<p>In order to permit implementations to deal with backwards-compatibility as they see fit, the behavior of this one ambiguous
|
|
construct was made undefined. (At least three implementations have been known to support this change already, so the degree of
|
|
change involved should not be great.)</p>
|
|
</li>
|
|
</ol>
|
|
|
|
<p>The <tt>'%'</tt> operator is the mathematical remainder operator when <b>scale</b> is zero. The behavior of this operator for
|
|
other values of <b>scale</b> is from historical implementations of <i>bc</i>, and has been maintained for the sake of historical
|
|
applications despite its non-intuitive nature.</p>
|
|
|
|
<p>Historical implementations permit setting <b>ibase</b> and <b>obase</b> to a broader range of values. This includes values less
|
|
than 2, which were not seen as sufficiently useful to standardize. These implementations do not interpret input properly for values
|
|
of <b>ibase</b> that are greater than 16. This is because numeric constants are recognized syntactically, rather than lexically, as
|
|
described in this volume of IEEE Std 1003.1-2001. They are built from lexical tokens of single hexadecimal digits and
|
|
periods. Since <blank>s between tokens are not visible at the syntactic level, it is not possible to recognize the
|
|
multi-digit "digits" used in the higher bases properly. The ability to recognize input in these bases was not considered useful
|
|
enough to require modifying these implementations. Note that the recognition of numeric constants at the syntactic level is not a
|
|
problem with conformance to this volume of IEEE Std 1003.1-2001, as it does not impact the behavior of conforming
|
|
applications (and correct <i>bc</i> programs). Historical implementations also accept input with all of the digits <tt>'0'</tt> -
|
|
<tt>'9'</tt> and <tt>'A'</tt> - <tt>'F'</tt> regardless of the value of <b>ibase</b>; since digits with value greater than or equal
|
|
to <b>ibase</b> are not really appropriate, the behavior when they appear is undefined, except for the common case of:</p>
|
|
|
|
<pre>
|
|
<tt>ibase=8;
|
|
/* Process in octal base. */
|
|
...
|
|
ibase=A
|
|
/* Restore decimal base. */
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>In some historical implementations, if the expression to be written is an uninitialized array element, a leading <space>
|
|
and/or up to four leading 0 characters may be output before the character zero. This behavior is considered a bug; it is unlikely
|
|
that any currently conforming application relies on:</p>
|
|
|
|
<pre>
|
|
<tt>echo 'b[3]' | bc
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>returning 00000 rather than 0.</p>
|
|
|
|
<p>Exact calculation of the number of fractional digits to output for a given value in a base other than 10 can be computationally
|
|
expensive. Historical implementations use a faster approximation, and this is permitted. Note that the requirements apply only to
|
|
values of <b>obase</b> that this volume of IEEE Std 1003.1-2001 requires implementations to support (in particular, not
|
|
to 1, 0, or negative bases, if an implementation supports them as an extension).</p>
|
|
|
|
<p>Historical implementations of <i>bc</i> did not allow array parameters to be passed as the last parameter to a function. New
|
|
implementations are encouraged to remove this restriction even though it is not required by the grammar.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_19"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_20"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="xcu_chap01.html#tag_01_10"><i>Grammar Conventions</i></a> , <a href="awk.html"><i>awk</i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_21"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 4.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_22"></a>Issue 5</h4>
|
|
|
|
<blockquote>
|
|
<p>The FUTURE DIRECTIONS section is added.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_09_23"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>Updated to align with the IEEE P1003.2b draft standard, which included resolution of several interpretations of the
|
|
ISO POSIX-2:1993 standard.</p>
|
|
|
|
<p>The normative text is reworded to avoid use of the term "must" for application requirements.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|