关于bc的用法
2009-06-02 21:19
155 查看
bc - An arbitrary precision calculator language[/b]
-i, --interactive
Force interactive mode. 交互式对话
# /* */ 注释用
A simple variable is just a
name 单个变量
and an array variable is specified as
name[expr] 数组
Boolean operations are also legal.
!expr The result is 1 if expr is 0.
expr && expr
The result is 1 if both expressions are non-zero.
expr || expr
The result is 1 if either expression is non-zero.
length ( expression )
The value of the length function is the number of significant digits in the expression.
read ( )
The read function (an extension) will 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. The value of the read function is the number read from the standard input using the current value of the variable ibase for the conversion base.
STATEMENTS
Statements (as in most algebraic languages) provide the sequencing of expression evaluation.
expression
string
print list
{ statement_list }
if ( expression ) statement1 [else statement2]
while ( expression ) statement
expression1;
while (expression2) {
statement;
expression3;
}
for ( [expression1] ; [expression2] ; [expression3] ) statement
break
continue
halt
return
return ( expression )
FUNCTIONS
define name ( parameters ) { newline
auto_list statement_list }
MATH LIBRARY 要调用这些数学函数,使用 –l 参数。
-l, --mathlib
Define the standard math library.
s (x) The sine of x, x is in radians.
c (x) The cosine of x, x is in radians.
a (x) The arctangent of x, arctangent returns radians.
l (x) The natural logarithm of x.
e (x) The exponential function of raising e to the value x.
j (n,x) The bessel function of integer order n of x.
EXAMPLES
1.
pi=$(echo "scale=10; 4*a(1)" | bc -l)
2.
scale = 20 # scale 定义 有效小数位数
bc [/b]預設僅輸出整數,如果要輸出小數點下位數,那麼就必須要執行[/b] scale=number [/b],那個[/b] number [/b]就是小數點位數[/b][/b]
/* Uses the fact that e^x = (e^(x/2))^2
When x is small enough, we use the series:
e^x = 1 + x + x^2/2! + x^3/3! + ...
*/
define e(x) {
auto a, d, e, f, i, m, v, z
/* Check the sign of x. */
if (x<0) {
m = 1
x = -x
}
/* Precondition x. */
z = scale;
scale = 4 + z + .44*x;
while (x > 1) {
f += 1;
x /= 2;
}
/* Initialize the variables. */
v = 1+x
a = x
d = 1
for (i=2; 1; i++) {
e = (a *= x) / (d *= i)
if (e == 0) {
if (f>0) while (f--) v = v*v;
scale = z
if (m) return (1/v);
return (v/1);
}
v += e
}
}
3.
scale=2
print "\nCheck book program!\n"
print " Remember, deposits are negative transactions.\n"
print " Exit by a 0 transaction.\n\n"
print "Initial balance? "; bal = read()
bal /= 1
print "\n"
while (1) {
"current balance = "; bal
"transaction? "; trans = read()
if (trans == 0) break;
bal -= trans
bal /= 1
}
quit
4.
define f (x) {
if (x <= 1) return (1);
return (f(x-1) * x);
}
5. 二、八、十、十六进制转化
[frost:/xin]# echo 'obase=16;ibase=2;1010'|bc
6. 一个递归的应用 IP
#!/usr/bin/bc
p=32;
define f(x) {
if(x<=1) return(p);
p--;
return f(x/2);
}
f(32);
后来全用awk 替代了。。。这些东西用的少了
-i, --interactive
Force interactive mode. 交互式对话
# /* */ 注释用
A simple variable is just a
name 单个变量
and an array variable is specified as
name[expr] 数组
Boolean operations are also legal.
!expr The result is 1 if expr is 0.
expr && expr
The result is 1 if both expressions are non-zero.
expr || expr
The result is 1 if either expression is non-zero.
length ( expression )
The value of the length function is the number of significant digits in the expression.
read ( )
The read function (an extension) will 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. The value of the read function is the number read from the standard input using the current value of the variable ibase for the conversion base.
STATEMENTS
Statements (as in most algebraic languages) provide the sequencing of expression evaluation.
expression
string
print list
{ statement_list }
if ( expression ) statement1 [else statement2]
while ( expression ) statement
expression1;
while (expression2) {
statement;
expression3;
}
for ( [expression1] ; [expression2] ; [expression3] ) statement
break
continue
halt
return
return ( expression )
FUNCTIONS
define name ( parameters ) { newline
auto_list statement_list }
MATH LIBRARY 要调用这些数学函数,使用 –l 参数。
-l, --mathlib
Define the standard math library.
s (x) The sine of x, x is in radians.
c (x) The cosine of x, x is in radians.
a (x) The arctangent of x, arctangent returns radians.
l (x) The natural logarithm of x.
e (x) The exponential function of raising e to the value x.
j (n,x) The bessel function of integer order n of x.
EXAMPLES
1.
pi=$(echo "scale=10; 4*a(1)" | bc -l)
2.
scale = 20 # scale 定义 有效小数位数
bc [/b]預設僅輸出整數,如果要輸出小數點下位數,那麼就必須要執行[/b] scale=number [/b],那個[/b] number [/b]就是小數點位數[/b][/b]
/* Uses the fact that e^x = (e^(x/2))^2
When x is small enough, we use the series:
e^x = 1 + x + x^2/2! + x^3/3! + ...
*/
define e(x) {
auto a, d, e, f, i, m, v, z
/* Check the sign of x. */
if (x<0) {
m = 1
x = -x
}
/* Precondition x. */
z = scale;
scale = 4 + z + .44*x;
while (x > 1) {
f += 1;
x /= 2;
}
/* Initialize the variables. */
v = 1+x
a = x
d = 1
for (i=2; 1; i++) {
e = (a *= x) / (d *= i)
if (e == 0) {
if (f>0) while (f--) v = v*v;
scale = z
if (m) return (1/v);
return (v/1);
}
v += e
}
}
3.
scale=2
print "\nCheck book program!\n"
print " Remember, deposits are negative transactions.\n"
print " Exit by a 0 transaction.\n\n"
print "Initial balance? "; bal = read()
bal /= 1
print "\n"
while (1) {
"current balance = "; bal
"transaction? "; trans = read()
if (trans == 0) break;
bal -= trans
bal /= 1
}
quit
4.
define f (x) {
if (x <= 1) return (1);
return (f(x-1) * x);
}
5. 二、八、十、十六进制转化
[frost:/xin]# echo 'obase=16;ibase=2;1010'|bc
6. 一个递归的应用 IP
#!/usr/bin/bc
p=32;
define f(x) {
if(x<=1) return(p);
p--;
return f(x/2);
}
f(32);
后来全用awk 替代了。。。这些东西用的少了
相关文章推荐
- 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法
- iphone关于self.用法的一些总结
- 关于TBitmap用法!
- 关于typeof 、instanceof 、constructor的用法
- 关于UITextFiled的用法汇总
- 关于layer-list在xml和代码中的用法ProgressBar
- 关于在bcb中调用外部程序的用法
- HTML、CSS里面关于 id、class、name 属性的区别和用法
- 关于typedef的用法总结
- C语言编译的顺序。关于 .c 文件 .h 文件的用法。Makefile文件简单概念。
- 一,关于MFC里面的EDIT Control控件的用法
- 关于typedef的用法总结
- 关于枚举的用法和类型转换
- 关于oracle with table as 创建临时表的用法示例以及使用with as 的优点
- python3.x 和 python2.x关于 urllib的用法
- asp.net中关于《%=》《%#》《%》 的用法——(转帖)
- 关于gluCylinder函数的用法…
- 关于全排列 next_permutation() 函数的用法
- 关于刷新页面和用法
- mysql中关于时间用法