Chinaunix首页 | 论坛 | 博客
  • 博客访问: 620458
  • 博文数量: 329
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 693
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-05 23:37
个人简介

Do not panic!

文章存档

2021年(1)

2018年(3)

2017年(7)

2016年(98)

2015年(220)

我的朋友

分类: LINUX

2016-08-17 19:48:28

原文地址:linux bc命令使用 作者:charming2440

bc 命令:
     bc 命令是用于命令行计算器。 它类似基本的计算器。 使用这个计算器可以做基本的数学运算。

语法:
  语法是 
     bc [命令开关]

命令开关:
     
-c仅通过编译。 bc命令的输出被发送到标准输出。
-l定义数学函数并且初始化值为20,取代默认值0。
filename文件名,它包含用于计算的计算器命令,这不是必须的命令。



示例:
     
  1. bc 
    输出:
    bc 1.06
    Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc.
    This is free software with ABSOLUTELY NO WARRANTY.
    For details type `warranty'.
    9*2
    18
    

    上述命令是来做数学运算。

  2. bc -l
    输出:
    bc 1.06
    Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc.
    This is free software with ABSOLUTELY NO WARRANTY.
    For details type `warranty'.
    1+2
    3
    

    上述命令是求'1+2'的和。

  3. bc calc.txt
    输出:
    bc 1.06
    Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc.
    This is free software with ABSOLUTELY NO WARRANTY.
    For details type `warranty'. 
    3
    

    'calc.txt' 这个文件有代码:1+2。 从文件输入并且显示输出结果。

    簡單好用的計算機: bc
    如果我想要使用簡單的計算器呢?很容易呀!就使用 bc 即可!在輸入 bc 之後, 顯示出版本資訊之後,就進入到等待指示的階段。如下所示:
    [root@linux ~]# bc
    bc 1.06
    Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
    This is free software with ABSOLUTELY NO WARRANTY.
    For details type `warranty'.
    _<==這個時候,游標會停留在這裡等待您的輸入
    事實上,我們是『進入到 bc 這個指令的工作環境當中』了! 就好像我們在 Windows 裡面使用『小算盤』一樣!所以,我們底下嘗試輸入的資料, 都是在 bc 程式當中在進行運算的動作。所以囉,您輸入的資料當然就得要符合 bc 的要求才行! 在基本的 bc 計算機操作之前,先告知幾個使用的運算子好了:
    • + 加法
    • - 減法
    • * 乘法
    • / 除法
    • ^ 指數
    • % 餘數
    • 好!讓我們來使用 bc 計算一些咚咚吧!
      [root@linux ~]# bc
      bc 1.06
      Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
      This is free software with ABSOLUTELY NO WARRANTY.
      For details type `warranty'.
      1+2+3+4  <==只有加法時
      10
      7-8+3
      2
      10*52
      520
      10%3     <==計算『餘數』
      1
      10^2
      100
      10/100   <==這個最奇怪!不是應該是 0.1 嗎?
      0
      quit     <==離開 bc 這個計算器
      在上表當中,粗體字表示輸入的資料,而在每個粗體字的底下就是輸出的結果。 咦!每個計算都還算正確,怎麼 10/100 會變成 0 呢?這是 因為 bc 預設僅輸出整數,如果要輸出小數點下位數,那麼就必須要執行 scale=number ,那個 number 就是小數點位數,例如:
      [root@linux ~]# bc
      bc 1.06
      Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
      This is free software with ABSOLUTELY NO WARRANTY.
      For details type `warranty'.
      scale=3     <==沒錯!就是這裡!!
      1/3
      .333
      340/2349
      .144
      quit
      好了!就是這樣子啦!簡單的很吧!以後你可以輕輕鬆鬆的進行加減乘除啦!

    MATH LIBRARY 
           If bc is invoked with the -l option, a math library is preloaded and the default  scale  is  set  to  20. 
           The  math  functions  will  calculate their results to the scale set at the time of their call.  The math 
           library defines the following functions: 

           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.  log函数(以2为底) 

           e (x)  The exponential function of raising e to the value x.  e的指数函数 

           j (n,x) 
                  The bessel function of integer order n of x.   贝塞尔函数 


    PS: echo "scale=100; a(1)*4" | bc -l  (计算圆周率)

    我试了,很神奇,可以计算圆周率到100位,而且速度很快
阅读(916) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~