日期:2014-05-16  浏览次数:20667 次

转载: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?