`

转载:linux下使用bc在命令行中进行计算

 
阅读更多

原文:http://www.basicallytech.com/blog/index.php?/archives/23-command-line-calculations-using-bc.html

bc是一个在命令行使用的强大计算器,本文作者通过实例来介绍bc命令的使用,是你瞬间掌握bc的基本应用。

文末,作者还给出了通过bc计算5000位PI的命令来测定你的cpu性能

--------------------------

 

If like me, you do most of your work from the command-line, using vim to edit files, mutt for e-mails, cd/ls/mv/find/etc instead of a file manager, then you may get annoyed by having to fire up a GUI calculator to make (what may sometimes be) a single calculation.

One useful feature of calculating on the command-line is that you can see what you've typed. For instance, sometimes when I'm entering a long, complex calculation on a calculator (either the GUI or the solid, hold-in-your-hand type), I sometimes forget if I've actually typed in all those numbers or made the calculations in the right order. Maybe it's just me ... :-)

This article shows how to quickly perform standard calculations on the command line including addition, subtraction, multiplication, division, square root, powers, conversion from decimal to hex, decimal to binary, hex to decimal, and binary to decimal. It also briefly introduces using bc in interactive mode and how to write files for use with bc for frequently repeated operations. There is a mention of using Google for performing calulations. It finishes with a little challenge to test the power of your CPU.

Other advantages of using bc include:

  • bc is included with (almost?) all Linux distros as standard, as well as (again, almost?) all Unices.
  • Results from calculations in some proprietary flavours of bchave up to 99 decimal digits before and after the decimal point. This limit has been greatly surpassed in GNU bc. I don't know what that limit is, but it's at least many, many tens of thousands. Certainly it's more than any GUI-based calculators (I've used) could accomodate.
  • You may also find yourself working in an environment where you simply don't have access to a GUI.
  • The syntax for basic sums is almost identical to Google's calculator function, so you can learn how to use two utilities in one go!

bc is a preprocessor for dc. The useful thing about bc is that it accepts input from files and from standard input. This allows us to pipe data to it for quick calculations.

Most of these examples follow a simple formula.

addition

echo '57+43' | bc

100

subtraction

echo '57-43' | bc

14

multiplication

echo '57*43' | bc

2451

scale

The scale variable determines the number of digits which follow the decimal point in your result. By default, the value of the scalevariable is zero. (Unless you use the -l option in which case it defaults to 20 decimal places. More about -l later.) This can be set by declaring scale before your calculation, as in the following division example:

division

echo 'scale=25;57/43' | bc

1.3255813953488372093023255

square root

echo 'scale=30;sqrt(2)' | bc

1.414213562373095048801688724209

This beats Google's calculator function which only calculates the result to 8 decimal places! ;-) Although Google's calculator function has this 8 decimal places limitation, it will allow imaginary numbers as answers.

power

echo '6^6' | bc

46656

parentheses

If you have read Robert Heinlein's The Number of the Beast, you may recall that the number of parallel universes in the story equals (six to the power of six) to the power of six. If you should try to calculate that like this:

echo '6^6^6' | bc

You will get a screen full of numbers (some 37374 digits), not the

10314424798490535546171949056

that you might expect.

If you're running a non-GNU version of bc, you'll most likely get something like:

exp too big
empty stack
save:args

The Google Calculator balks at '6^6^6' as well. Good ol' GNU.

That's because you typed the wrong question. You need to type:

echo '(6^6)^6' | bc

Whereas what you did type was interpreted as:

echo '6^(6^6)' | bc

which is an entirely different number altogether. So the positioning of parentheses (brackets to you and me!) is very important. I use brackets to separate the different components of my sums whenever possible, just eliminate any possible doubt that I may get the wrong answer. Consider the following calculations:

echo '7+(6*5)' | bc

echo '7+6*5' | bc

echo '6*5+7' | bc

They all give the same answer, 37, but I would have typed the first calculation, unless of course, I meant:

echo '(7+6)*5' | bc

Or to put it another way:

echo '13*5' | bc

which is 65.

obase and ibase

obase and ibase are special variables which define output and input base.

Legitimate obase values range from 2 to 999, although anything beyond 16 is wasted on me!

Legitimate ibase values range from 2 to 16.

Some examples will explain all this better.

convert from decimal to hexadecimal

Here we're converting 255 from base 10 to base 16:

echo 'obase=16;255' | bc

FF

convert from decimal to binary

And here we're converting the number 12 from base 10 to base 2:

echo 'obase=2;12' | bc

1100

Which reminds me of the old joke:

There are only 10 types of people in the world -- those who understand binary, and those who don't.

Which leads us neatly onto the next example:

convert from binary to decimal

Here we're converting the binary number 10 to a base 10 (decimal) number.

echo 'ibase=2;obase=A;10' | bc

2

Note that the obase is "A" and not "10". Sorry, you've got to learn some hex. The reason for this is you've set the ibase to "2", so if you now had tried to use "10" as the value for the obase, it would stay as "2", because "10" in base 2 is "2". So you need to use hex to "break out" of binary mode.

Well, that was just to explain the joke; now something a bit more challenging:

echo 'ibase=2;obase=A;10000001' | bc

129

convert from hexadecimal to decimal

echo 'ibase=16;obase=A;FF' | bc

255

Again, note the use of "A" to denote base 10. That is because "10" in hex (base 16 - the ibase value) is 16.

a brief introduction to interactive mode

You can also run bc in interactive mode:

bc

If you're running GNU bc, you should get the following notice:

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'.

Followed by an uninviting blank prompt. Otherwise you'll just get an uninviting blank prompt.

If you wish to get straight to the uninviting blank prompt, use the-q option, which runs bc in quiet mode, preventing the normal GNU bc welcome from being printed:

bc -q

Using the basics we've been through from the examples above, enter a calculation:

scale=5
57/43
1.32558

Type quit to exit bc interactive mode.

using bc with shell scripts

You can use shell variables with bc, which is very useful in shell scripts:

FIVE=5 ; echo "$FIVE^2" | bc

25

Note the use of double-quotes to preserve the value of the variable$FIVE.

a brief introduction to using bc with files

Using bc with files allows complex calculations to be repeated, again and again, a bit like using a spreadsheet to run the same calculations on changing figures ... but faster.

Here is a simple example:

scale=2

/* C-style comments
   are allowed, as are spaces */

print "\nConvert Fahrenheit to Celsius\n\n"
print "Temperature in Fahrenheit: " ; fah = read()
print "\n"
print "Equivalent Temperature in Celsius is: "
(fah - 32.0) * 5.0 / 9.0
quit

Create and save the file, then run it like this:

bc -q filename

Convert Fahrenheit to Celsius

Temperature in Fahrenheit: 61

Equivalent Temperature in Celsius is: 16.11

Note that this example has only been tested with GNU bc. Other (proprietary) versions of bc may have more stringent syntax requirements. Some bcs don't allow the use of print or read, for example, so you have to edit your file before each calculation. Not very useful.

a quick challenge for your PC (GNU bc only)

If you wish to test the comparative speed of your PC, try this challenge: use bc to calculate Pi to 5000 decimal places. The idea for this challenge came from a great article at Geekronomicon.

If you really want to tie up your machine for an hour (or more), you could try the "Pi to 25000 decimal places" challenge from the aforementioned Geekronomicon;-)

First, to put things in perspective, here is some information about my CPU:

cat /proc/cpuinfo | egrep "model name|MHz"

model name      : AMD Athlon(tm) 64 Processor 3500+
cpu MHz         : 2211.346

Note the use (below) of the command bc -l -q
-l loads the math library which is required for the use of the "a" (arctangent) in the calculation for Pi. You can learn more about the math library functions in the bc command manual
I'm not sure what effect the -q option (quiet, no welcome message printed) has on our test, but I guess it can't harm.

time echo "scale=5000; 4*a(1)" | bc -l -q

3.141592653589793238462643383279502884197169399375105820974944592307\
...
...
...
73774418426312986080998886874132604720

real    0m44.164s
user    0m44.099s
sys     0m0.008s

44.099 seconds! Not bad. :-) I imagine that some Gentoo folks may be interested to see what difference their compile-time optimisations make to the speed of bc. FWIW, my distro of choice isArch Linux.

useful links

GNU bc manual

Google's calculator function

分享到:
评论

相关推荐

    linux下提取的 可在windows下 使用的 高精度计算 bc、qc单文件命令行版

    标题中的“linux下提取的 可在windows下 使用的 高精度计算 bc、qc单文件命令行版”指的是两个在Linux环境下广泛使用的高精度计算工具——bc和qc,它们已经被移植到Windows系统,并以可执行文件的形式(bc.exe和dc....

    在 Linux 中使用 ImageMagick 命令行操作图像文件.docx

    在 Linux 中使用 ImageMagick 命令行操作图像文件,可以无需打开图像编辑器进行修改图像文件。convert 命令包含在 ImageMagick 安装包里。ImageMagick 可以安装在 Linux、Mac OS X、Windows 系统中。下载链接为:...

    GNU BC FOR WINDOWS(命令行计算器-任意精度)

    总的来说,GNU BC for Windows是Windows环境下进行复杂计算的有力工具,特别是在批处理脚本和自动化任务中,它的强大功能和灵活性能够极大地提高工作效率。如果你需要处理超出常规计算能力的数学问题,或者希望在...

    Linux命令之bc--浮点计算器、进制转换

    在Linux环境中,用户经常需要处理各种计算任务。尽管Bash环境已经具备了处理整数运算的能力,但对于涉及浮点数的复杂计算,则显得力不从心。这时,`bc` 命令就派上了用场。`bc` 是一个任意精度计算器语言,能够处理...

    bc-1.06 源码

    在这个源码包中,我们可以深入理解 `bc` 的内部工作原理,并学习如何在Linux环境下编译和安装开源软件。 ### 1. bc 命令简介 `bc` 命令是Linux中的一个实用程序,它提供了一个类似于Lisp的语法,允许用户执行算术...

    bc-1.06 linux系统下自带的command-line计算器程序源代码

    在Linux操作系统中,`bc`是一个强大的命令行计算器程序,它允许用户进行算术运算、逻辑操作以及执行脚本。`bc`不仅支持基本的算术运算,还提供了丰富的数学函数和控制结构,使得它成为一个功能全面的计算环境。这份...

    怎么样称得上是Linux下优秀的命令行计算器

    Linux下的优秀命令行计算器,尤其是GNU bc,是一个强大的工具,尤其适合那些经常在终端环境中工作的用户。GNU bc起源于70年代的Unix时代,起初是一个类似C语言的编程语言,随着时间的发展,它演变为今天的POSIX bc,...

    linux下的计算器使用

    ### Linux下的计算器使用详解 #### 引言 在Linux系统中,用户经常需要进行各种计算任务,例如数学运算、进制转换等。虽然Windows系统下的`calc.exe`因其直观的图形界面而广为人知,但对于命令行爱好者或者需要批量...

    Linux下的bc计算器移植到windows下

    自己从linux下移植到windows下的bc-1.06,非常好用的。

    bc_1.06.95.orig.tar.gz

    标题中的"bc_1.06.95.orig.tar.gz"是一个典型的开源软件发布格式,常见于Linux和Unix-like系统中。这个文件名揭示了几个关键信息:`bc`是软件的名字,`1.06.95`是版本号,`orig.tar`表示原始源代码已打包为tar归档,...

    中国移动自主研发BC-Linux操作系统 (1).pdf

    BC-Linux已经在中国移动总部、各省公司及专业公司、多个外部单位的几十个现网项目中得到广泛应用,已完成装机量超过1万台,运行状态良好。预计,2017年BC-Linux装机量将突破2万台。 随着5G时代的到来,BC-Linux也将...

    LINUX 实验指导

    实验六:Linux下的shell编程 在这一实验中,你将学习编写简单的shell脚本,包括变量、条件语句、循环结构和函数的使用。通过编写和执行shell脚本,你可以自动化日常的系统管理任务,提高工作效率。 实验七:Linux下...

    Lab 6:Linux系统调用1

    在本实验“Lab 6:Linux系统调用1”中,我们将深入理解Linux操作系统的核心机制,特别是如何实现自定义的系统调用。这个过程涉及到多个步骤,包括获取源码、配置内核、编译以及安装系统调用。以下是对整个流程的详细...

    The-Linux-foundation.zip_foundation

    - Linux终端下的菜单系统通常通过shell脚本实现,如使用`select`命令在bash中创建交互式菜单。 - 学习如何接收用户输入,根据选择执行不同命令或脚本,理解条件判断和流程控制。 5. **文件分类**: - 文件分类...

    Linux操作系统下C++编程

    在Windows中,用户通常使用像Turbo C++(TC),Borland C++(BC)或Visual C++(VC)这样的集成开发环境(IDE),它们提供图形化界面,简化了编程过程。而在Linux环境下,开发工作更多地依赖于命令行工具,如GCC...

    bc31使用說明

    为了方便启动bc31,需要将`C:\BC\BIN`目录添加到系统环境变量`PATH`中,这样可以在任意目录下通过命令行方式启动bc31。 启动bc31的方法通常是:首先打开MS-DOS窗口,切换到自己的工作目录(例如`D:\NetBox2\Step1>`...

    BC.rar_bc++

    在BC++中,用户可以编写简单的脚本进行计算,也可以通过它来理解和实践编程的基本概念。它支持变量定义、函数定义、输入输出操作等,这对于初学者来说是理解编程逻辑的一个很好的工具。BC++通常包含了BC的标准功能,...

Global site tag (gtag.js) - Google Analytics