bc:

bc 是一种高精度的可交互执行的计算机语言。它在一些浮点数的运算中应用广泛。

一般情况下我们直接输入 bc ,便可进入其工作环境。当然,它还有其他的参数

-h

显示帮助信息并退出

-i

强制进入交互模式

-l

定义了标准的数学库

-w

使用扩展时给出警告

-s

使用标准语言

-q

不打印欢迎信息

-v

打印版本信息并退出

在bc中大多数的元素是数字,他们都有两种属性:长度与精度(小数点位数)

bc有4中特殊的变量

scale

精度(小数点之后的位数)

ibase

输入进制数

obase

输出进制数

last

存储上个指令输出的结果

注释: /*……*/ 或者 #

支持的变量(var)与表达式(expr)

++/-- var

先加(减)后取值

var ++/--

先取值后加减

-expr

取反

expr+/-/*///%/^expr

加减乘除求余乘方

(expr)

布尔值

var=expr

 

var<op>= expr

var=var<op>expr

expr1 </<=/>/>=/==/!=/expr2

判断语句

!expr

取反

expr &&/|| expr

逻辑运算

length( expression )

计算长度

read()

读取输入

scale( expression )

精确度?

sqrt( expression )

平方根

可调用的语句

print " ** "

打印

{ 语句集合 }

 

if (expression ) ** [else **]

条件判断

while( expression ) **

循环

for([ exp1] ;[exp2] ; [exp3]) **

循环

break

可跳出循环

continue

进行下次循环

halt

退出bc

return

函数返回

其他

limits

显示limits

quit

退出

warranty

显示bc相关信息

函数定义方法

define name ( parameters ) { newline

auto_list statement_list } 这个部分请看实例2或者man帮助来进一步了解。

关于数组 name[]

数学库函数

s(x)

sin(x) x是弧度

c(x)

cos(x)

a(x)

arctan(x)

l(x)

ln(x)

e(x)

ex

j(n,x)

贝塞尔函数的整数n x。

实例1

计算圆周率

[linux@linux ~]$ pi=$(echo "scale=10; 4*a(1)" | bc -l)

[linux@linux ~]$ echo $pi

3.1415926532

实例2

scale = 20

/* 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

创建test.bc

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

[linux@linux ~]$ bc -q test.bc

Check book program!

Remember, deposits are negative transactions.

Exit by a 0 transaction.

Initial balance? 3

current balance = 3.00

transaction? 5

current balance = -2.00

transaction? 0

[linux@linux ~]$

通常在Bash脚本中使用bc的范例格式为:
variable=$(echo "OPTIONS; OPERATIONS" | bc [options]) 即:echo "[选项];操作" | bc [选项]

可参考实例1

[linux@linux ~]$ echo "obase=8; ibase=16; 11+a"|bc

21

下面这个实例可参考下书写格式

使用bc命令的脚本片段
Bash代码 
# usage: add_sum <num1> <num2> 
# 计算两个数的和 
add_sum() 

bc -q <<EOF 
$1+$2 
EOF 
}

bc命令就介绍到这了,如果朋友有更好的实例,希望能多多分享。

bc命令详解与实例的更多相关文章

  1. groupadd命令详解(实例)

     groupadd命令详解(实例)  1.作用groupadd命令用于将新组加入系统. 2.格式groupadd [-g gid] [-o]] [-r] [-f] groupname 3.主要参数-g ...

  2. Linux下的压缩zip,解压缩unzip命令详解及实例

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

  3. Linux下的压缩解压缩命令详解及实例

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

  4. bc命令详解

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/lovevivi/p/4359296.html 最近经常要在linux下做一些进制转换,看到了可以使用bc命令,如下: ...

  5. JavaScript中的execCommand()命令详解及实例展示

    execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其 ...

  6. Linux crontab命令详解与实例

    内容有重复的,不过本着宁多勿少的原则就都看看吧,就当加深印象啦 基本格式 :* * * * * command分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示第2列表示小时 ...

  7. unzip/tar命令详解

    博客目录总纲首页 原文链接:https://www.cnblogs.com/zdz8207/p/3765604.html Linux下的压缩解压缩命令详解及实例 实例:压缩服务器上当前目录的内容为xx ...

  8. apt-get 命令详解(中文),以及实例

    apt-get 命令详解(中文),以及实例 一,什么的是apt-get 高级包装工具(英语:Advanced Packaging Tools,简称:APT)是Debian及其衍生发行版(如:ubunt ...

  9. Windows WMIC命令使用详解(附实例)

    第一次执行WMIC命令时,Windows首先要安装WMIC,然后显示出WMIC的命令行提示符.在WMIC命令行提示符上,命令以交互的方式执行 执行“wmic”命令启动WMIC命令行环境.这个命令可以在 ...

随机推荐

  1. Blue Jeans(串)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10083   Accepted: 4262 Description The ...

  2. Light OJ 1005 - Rooks(DP)

    题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这 ...

  3. 动态规划(水题):COGS 261. [NOI1997] 积木游戏

    261. [NOI1997] 积木游戏 ★★   输入文件:buildinggame.in   输出文件:buildinggame.out   简单对比时间限制:1 s   内存限制:128 MB S ...

  4. 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  5. socket(tcp)互发信息

    一:有图有真相,很简单 a, b, Thread 构造函数(ParameterizedThreadStart)初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. 参数star ...

  6. Rotate List —— LeetCode

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. Swap Nodes in Pairs——LeetCode

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  8. wordpress 404 error on all pages!

    You have to enable mod_rewrite in apache itself or you won't be able to have permalinks the way you ...

  9. 【转】四种常见的POST提交数据方式

    HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...

  10. [Angular 2] Exposing component properties to the template

    Showing you how you can expose properties on your Controller to access them using #refs inside of yo ...