Unix-commands-bc

提供:Dev Guides
移動先:案内検索

bc-Unix、Linuxコマンド

NAME

*bc* -任意精度の計算機言語。

概要

bc options file...

説明

bcは、ステートメントの対話型実行で任意の精度の数値をサポートする言語です。 コマンドラインにリストされているすべてのファイルのコードを、リストされている順序で処理することから始まります。 すべてのファイルが処理された後、bcは標準入力から読み取りを開始します。 すべてのコードは読み取られたとおりに実行されます。

bcは通常、シェルスクリプト内で使用され、「here」ドキュメントを使用してプログラムの詳細をbcに渡します。

オプション

Tag Description
--help Display help
-h, --help Print the usage and exit.
file A file containing the calculations/functions to perform. This can be piped from standard input.
-i, --interactive Force interactive mode.
-l, --mathlib Define the standard math library.
-w, --warn Give warnings for extensions to POSIX bc.
-s, --standard Process exactly the POSIX bc language.
-q, --quiet Do not print the normal GNU bc welcome.
-v, --version Print the version number and copyright and quit.

bcを使用するユーティリティクラスを作成します。

#bcsample
if [ $# != 1 ]
   then
   echo "A number argument is required"
   exit
fi

bc <<END-OF-INPUT
scale=6
/*first we define the function*/
define myfunc(x){
   return(sqrt(x) + 10);
}

/* then use the function to do the calculation*/
x=$1
"Processing";x;" result is ";myfunc(x)
quit
END-OF-INPUT

echo "(to 6 decimal places)"

ユーティリティを実行する

$ chmod a+x bcsample
$ ./bcsample 25
Processing 25 result is 15

bcがサポートする標準機能

Function Description
length ( expression ) The value of the length function is the number of significant digits in the expression.
read ( ) Read a number from the standard input, regardless of where the function occurs. Beware, this can cause problems with the mixing of data and program in the standard input. The best use for this function is in a previously written program that needs input from the user, but never allows program code to be input from the user.
scale ( expression ) The number of digits after the decimal point in the expression.
sqrt ( expression ) The number of digits after the decimal point in the expression.
++ var increment the variable by one and set the new value as the result of the expression.
-var ++ The result of the expression is the value of the variable and the variable is then incremented by one.
 — var decrement the variable by one and set the new value as the result of the expression.
var —  The result of the expression is the value of the variable and the variable is then decremented by one.
( expr ) Parenthesis alter the standard precedence to force the evaluation of an expression.
var = expr The variable var is assigned the value of the expression.

+-/*%^、関係式、ブール演算などのほとんどの標準的な数式がサポートされています。

コメント

/インラインコメント/-インラインコメントに使用

#単一行コメント。 -単一行のコメントに使用されます。 行末文字はコメントの一部とは見なされず、正常に処理されます。

link:/cgi-bin/printpage.cgi [__印刷]