bc -- The Linux Command-line Calculator
Probably every distro of desktop Linux comes with its own built-in, graphical calculator. And, if you don't like it, you can always install another one from the distro's repository. So, why would you want to learn about a calculator that has no graphical interface, and that you work simply by entering formulas on the command-line?
Well, you might be working with a computer that isn't running a graphical interface. Or, you might just want something that works a bit faster than what a graphical-type calculator would. Most importantly, you might want something that's more flexible, and more powerful than any run-of-the-mill graphical-type calculator.
And if you really do want speed, flexibility, and power, have we got a deal for you!
The "bc" calculator comes as a part of your Linux distro, so there's no need for you to install anything extra. In addition to performing simple math functions, it can also perform conversions between different number systems, perform a number of scientific math functions, and can even run programs that you write and save in a text file. Here's how it works.
At the command-prompt, enter:
bc -l
(You'll need the "-l" switch to perform anything other than the simplest of functions. It's what pulls in the "standard math libraries".)
Your screen should now look something like:
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'.
Now, just type in your formula at the blinking cursor, and hit "Enter". You can type in something simple, or you can type in complex statements with grouped operators.
((83.12 + 32.13) * 37.3)
4298.825
In addition to the normal math functions--addition, subtraction, multiplication, division, modulus, and exponents--bc also has functions to calculate sines, cosines, arctangents, and logarithms. So, for example, if you need to find the natural logarithm of 4,332, it's as simple as:
l (4332)
8.37378460812088123024
The "ibase" function allows you to set what numbering system that you want to use for input. "obase" allows you to set what numbering system to use for output. You can mix-n'-match, here, so that you can have a handy way of converting numbers from one system to another.
Let's say that you want to convert numbers from decimal to hexadecimal. First, set your "obase".
obase = 16
Now, multiply the number that you want to convert by 1:
14 * 1
E
Oh, but wait, you really wanted that answer in binary. No problem, just change the obase.
obase = 2
14 * 1
1110
To return to normal, set the obase back to 10.
obase = 10
14 * 1
14
To convert from another number system back to decimal, set the "ibase".
ibase = 16
E * 1
14
You can perform calculations in other number systems by setting both the ibase and the obase.
ibase = 2
obase = 2
101 + 1
110
Now, here's the real beauty of bc. You can write programs for it, save them to text files, and then run the programs by using the name of the program text file as an argument. (The programming syntax is somewhat similar to that of bash scripting.) Let's start by looking at a program that demonstrates the use of the "if" construct. We'll save the program to a file that we'll name "simple_if".
print "Input x\n"; x = read()
print "Input y\n"; y = read()
if (x > y) {
print "x > y\n";
}
if (x < y) {
print "x < y\n";
}
if (x == y) {
print "x = y\n";
}
quit
Now, let's run it, and see what happens.
bc -l simple_if
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'.
Input x
25
Input y
4
x > y
The "read()" functions in the first two lines allowed us to interactively assign values to the "x" and "y" variables, and the "if" constructs chose the appropriate output.
For something more practical, let's look at the "checkbook" example program from the bc man page.
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
The "scale" function on the first line limits input and output to two decimal places. When you run it, it'll first ask you to enter an initial account balance. The "while" construct will allow you to enter transactions until you're done, at which time you'll enter a transaction of "0".
bc -l checkbook
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'.
Check Book Program!
Remember, deposits are negative transactions.
Exit by a 0 transaction.
Initial balance?5578.80
current balance =5578.80
transaction?-1230
current balance =6808.80
transaction?45
current balance =6763.80
transaction?34.89
current balance =6728.91
transaction?5089.90
current balance =1639.01
transaction?-55.15
current balance =1694.16
transaction?1608.97
current balance =85.19
transaction?0
For more information, enter "man bc" at the command-line.
< Prev | Next > |
---|