📄 bc.html
字号:
The result will be the quotient of the two expressions.Thescale of the result will be the value of <b>scale</b>.<dt><i>expression</i> % <i>expression</i><dd>For expressions<i>a</i>and<i>b</i>,a % bwill be evaluated equivalent to the steps:<ol><li>Computea/bto current scale.<li>Use the result to compute:<pre><code>a - (a / b) * b</code></pre>to scale:<pre><code>max(scale + scale(b), scale(a))</code></pre></ol>The scale of the result will be:<pre><code>max(scale + scale(b), scale(a))</code></pre>When<b>scale</b>is zero, the "%" operator is the mathematical remainder operator.</dl><p>The additive operators ("+", "-") bind left to right.<dl compact><dt><i>expression</i> + <i>expression</i><dd>The result will be the sum of the two expressions.The scale ofthe result will be the maximum of the scales of the expressions.<dt><i>expression</i> - <i>expression</i><dd>The result will be the difference of the two expressions.Thescale of the result will be the maximum of the scales of theexpressions.</dl><p>The assignment operators ("=", "+=", "-=", "*=", "/=", "%=", "^=")bind right to left.<dl compact><dt><i>named-expression</i> = <i>expression</i><dd>This expression results in assigning the value of theexpression 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<i>expression</i>.</dl><p>The compound assignment forms:<pre><code><i>named-expression <operator></i>= <i>expression</i></code></pre>are equivalent to:<pre><code><i>named-expression</i> = <i>named-expression <operator> expression</i></code></pre>except that the<i>named-expression</i>will be evaluated only once.<p>Unlike all other operators, the relational operators("<", ">", "<=", ">=", "==", "!=")will be only valid as the object of an <b>if</b>, <b>while</b>or inside a<b>for</b>statement.<dl compact><dt><i>expression1</i> < <i>expression2</i><dd>The relation will be true if the value of<i>expression1</i>isstrictly less than the value of <i>expression2</i>.<dt><i>expression1</i> > <i>expression2</i><dd>The relation will be true if the value of<i>expression1</i>isstrictly greater than the value of <i>expression2</i>.<dt><i>expression1</i> <= <i>expression2</i><dd>The relation will be true if the value of<i>expression1</i>is lessthan or equal to the value of <i>expression2</i>.<dt><i>expression1</i> >= <i>expression2</i><dd>The relation will be true if the value of<i>expression1</i>isgreater than or equal to the value of <i>expression2</i>.<dt><i>expression1</i> == <i>expression2</i><dd>The relation will be true if the values of<i>expression1</i>and<i>expression2</i>are equal.<dt><i>expression1</i> != <i>expression2</i><dd>The relation will be true if the values of<i>expression1</i>and<i>expression2</i>are unequal.</dl><p>There are only two storage classes in<i>bc</i>,global and automatic(local).Only identifiers that are to be local to a function needbe declared with the<b>auto</b>command.The arguments to a functionwill be local to the function.All other identifiers are assumed tobe global and available to all functions.All identifiers, globaland local, have initial values of zero.Identifiers declared asauto will be allocated on entry to the function and released on returningfrom the function.They therefore do not retain values betweenfunction calls.Auto arrays will be specified by the array name followedby empty square brackets.On entry to a function,the old values of the names that appear as parameters and as automaticvariables are pushed onto a stack.Until the function returns,reference to these names refers only to the new values.<p>References to any of these names from other functions thatare called from this function also refer to the new valueuntil one of those functions uses the same name for a local variable.<p>When a statement is an expression, unless the main operator isan assignment, execution of the statement will write the valueof the expression followed by anewlinecharacter.<p>When a statement is a string, execution of the statementwill write the value of the string.<p>Statements separated by semicolons ornewline characterswill be executed sequentially.In an interactive invocation of<i>bc</i>,each time anewlinecharacter is read that satisfies the grammatical production:<pre><code>input_item : semicolon_list NEWLINE</code></pre>the sequential list of statements making up the<b>semicolon_list</b>will be executed immediately and any output produced by thatexecution will be written without any delay due to buffering.<p>In an<b>if</b>statement(<b>if</b> (<i>relation</i>) <i>statement</i>), the<i>statement</i>will be executed if the relation is true.<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>will be executed and the<i>relation</i>retested.When the<i>relation</i>is false, execution will resume after<i>statement</i>.<p>A<b>for</b>statement(<b>for</b> (<i>expression</i>; <i>relation</i>; <i>expression</i>) <i>statement</i>)is the same as:<pre><code><i>first-expression</i>while (<i>relation</i>) { <i>statement</i> <i>last-expression</i>}</code></pre>All three expressions must be present.<p>The<b>break</b>statement causes terminationof a<b>for</b>or<b>while</b>statement.<p>The<b>auto</b>statement(<b>auto</b> <i>identifier</i><b>[</b>,<i>identifier</i><b>]</b> ...)will cause the values of the identifiers to be pushed down.The identifiers can beordinary identifiers or array identifiers.Array identifiers arespecified by following the array name by empty square brackets.The<b>auto</b>statement must be the first statement in a function definition.<p>A<b>define</b>statement:<pre><code>define <i>LETTER</i> ( <i>opt_parameter_list</i> ) { <i>opt_auto_define_list</i> <i>statement_list</i>}</code></pre>defines a function named<i>LETTER</i>.If a function named<i>LETTER</i>was previously defined, the<b>define</b>statement will replace the previous definition.The expression:<pre><code><i>LETTER</i> ( <i>opt_argument_list</i> )</code></pre>will invoke the function named<i>LETTER</i>.The behaviour is undefinedif the number of arguments in the invocation does not match thenumber of parameters in the definition.Functions will be defined before they are invoked.A function will be considered to bedefined within its own body, so recursive calls are valid.The values of numeric constants within a function will beinterpreted in the base specified by the value of the<b>ibase</b>register when the function is invoked.<p>The<b>return</b>statements(<b>return</b> and <b>return</b>(<i>expression</i>)) will causetermination of a function, popping of its auto variables andspecifies the result of the function.The first form is equivalent toreturn(0).The value and scale of an invocation of the function will be thevalue and scale of the expression in parentheses.<p>The<b>quit</b>statement (<b>quit</b>) will stop execution of a<i>bc</i>program at thepoint where the statement occurs in the input, even if it occursin a function definition, or in an<b>if</b>,<b>for</b>or<b>while</b>statement.<p>The following functions will be defined when the<b>-l</b>option is specified:<dl compact><dt><b>s</b> ( <i>expression</i> )<dd>Sine of argument in radians.<dt><b>c</b> ( <i>expression</i> )<dd>Cosine of argument in radians.<dt><b>a</b> ( <i>expression</i> )<dd>Arctangent of argument.<dt><b>l</b> ( <i>expression</i> )<dd>Natural logarithm of argument.<dt><b>e</b> ( <i>expression</i> )<dd>Exponential function of argument.<dt><b>j</b> ( <i>expression</i> , <i>expression</i> )<dd>Bessel function of integer order.</dl><p>The scale of an invocation of each of these functions will be thevalue of the<b>scale</b>register when the function is invoked.The behaviour is undefined if any of these functions isinvoked with an argument outside the domain of the mathematical function.</blockquote><h4><a name = "tag_000_000_166"> </a>EXIT STATUS</h4><blockquote>The following exit values are returned:<dl compact><dt>0<dd>All input files were processed successfully.<dt><i>unspecified</i><dd>An error occurred.</dl></blockquote><h4><a name = "tag_000_000_167"> </a>CONSEQUENCES OF ERRORS</h4><blockquote>If any<i>file</i>operand is specified and the named file cannot be accessed,<i>bc</i>will write a diagnostic message to standarderror and terminate without any further action.<p>In an interactive invocation of<i>bc</i>,the utility shouldprint an error message and recover following any error in the input.In a non-interactive invocation of<i>bc</i>,invalid input causes undefined behaviour.</blockquote><h4><a name = "tag_000_000_168"> </a>APPLICATION USAGE</h4><blockquote>Automatic variables in<i>bc</i>do not work in exactly the same way as in either C or PL/1.<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 canreact to error messages) or by application programsthat can somehow validate the answers returnedas not including error messages.<p>The<i>bc</i>utility always uses the period(.)character to represent aradix point, regardless of any decimal-point character specifiedas part of the current locale.In languages like C or<i><a href="awk.html">awk</a></i>,the period character is used in program source, so it can beportable and unambiguous, while the locale-specific character isused in input and output.Because there is no distinctionbetween source and input in<i>bc</i>,this arrangement would not be possible.Using the locale-specific character in<i>bc</i>'sinput would introduce ambiguities into the language; consider thefollowing example in a locale with a comma as the decimal-point character:<pre><code>define f(a,b) { ...}...f(1,2,3)</code></pre><p>Because of such ambiguities, the period character is used in input.Having input follow different conventions from outputwould be confusing in either pipeline usage or interactiveusage, so the period is also used in output.</blockquote><h4><a name = "tag_000_000_169"> </a>EXAMPLES</h4><blockquote>In the shell, the following assigns an approximation of the first ten digitsof to the variable<i>x</i>:<pre><code>x=$(printf "%s\n" 'scale = 10; 104348/33215' | bc)</code></pre><p>The following<i>bc</i>program prints the same approximation of ,with a label, to standard output:<pre><code>scale = 10"pi equals "104348 / 33215</code></pre><p>The following defines a function to compute an approximate value ofthe exponential function(note that such a function is predefined if the<b>-l</b>option is specified):<pre><code>scale = 20define 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 }}</code></pre><p>The following prints approximate values of the exponential function ofthe first ten integers:<pre><code>for (i = 1; i <= 10; ++i) { e(i)}</code></pre></blockquote><h4><a name = "tag_000_000_170"> </a>FUTURE DIRECTIONS</h4><blockquote>The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns about parts of this interfacedefinition to the IEEE PASC Shell and Utilities Working Group which is identifying the corrections.A future revision of this specification will align withIEEE Std. 1003.2b when finalised.</blockquote><h4><a name = "tag_000_000_171"> </a>SEE ALSO</h4><blockquote><i><a href="awk.html">awk</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -