Adding 3 + 5, Part V: bc
[Uh oh! 10 minutes and I'm just starting.]
Today is part V in my series on different ways that Mac OS X can do a simple mathematical operation. While our example 3 + 5 is pretty simple, I've tried to focus on the pros and cons of each strategy. I say that because today's solution is the least elegant that I will present in this series. This solution requires brining up the dreaded Terminal window and running *nix command.
But don't panic. It's an easy thing to do.
Open a new Terminal app window.
When you get the prompt type in: bc -l - that is, type in the the lower-case version of BC -L
You'll get a few lines of text and then no prompt but bc is running in the window. The -l flag is not needed for our simple example but I always add it because it seems to cost nothing and makes bc do more.
Now type in: 3 + 5
Hit enter.
Voila. The answer appears.
So you may be asking yourself why I would bother showing you such a nasty looking calculator. It's a fair question.
The answer is that this ugly, nasty-looking calculator is one of the most powerful calculators I have ever used. It's not programmable and it won't graph. But it will do arbitrary precision calculations and that is very rare. And it will do them incredibly quickly.
Arbitrary precision is easier to explain by explaining its opposite, fixed precision. Open up the Calculator app that was discussed in part I of this series. Then divide 3 by 7. Depending on the settings you have in Calculator's View-->Decimal Places menu. You will see up to 15 digits to the right of the decimal point. While most of the time 15 digits is more than enough, what do you do if you ever need 20, or 50, or 100?
That's where bc shines. Go back to your Terminal app and divide 3 by 7. I think you get 20 digits by default. No problem. Copy and paste these lines into bc:
scale=100
3/7
I guess 100 digits of precision isn't that impressive for a computer for division. But how about this one:
scale=1000
4*a(1)
First, I'm no math wiz. I stole that directly from the bc man page. It's on your computer but if you don't know how to get it, just look at this online version. And, in case you're wondering, little bc just calculated the first 1,000 digits of pi. On my computer it takes about a half second to do that.
While my examples have only involved numbers with large decimal portions, setting bc's scale variable works on the non-decimal places as well. So if you want to calculate some really large number, bc is there as well.
Unless you're trying to impress your friends by memorizing 1,000 digits of pi, you probably won't need bc very often. But it is a very handy tool when you do need it.
Also, bc is probably the easiest of all the calculators we have looked at to copy-and-paste the input or output. Sometimes that can be helpful.
I think tomorrow will be the last in this series. Please let me know if you have other ways of doing math on the Mac. I can always add another part in a week or two.
Related articles
What is the square root of 65 to 1 decimal place (wiki.answers.com) This is an interesting example. bc will do this just by copy-and-pasting the two lines below. This means you can get dumber as long as you remember how to read the bc man page to find out that scale is the number of decimal places and that sqrt is the function for square root.
scale=1
sqrt(65)
Adding 3 + 5, Part II: The Dashboard (mactipaday.neilsmithline.com)
Adding 3 + 5, Part I: Calculator.app (mactipaday.neilsmithline.com)
Adding 3 + 5, Part III: Spotlight (mactipaday.neilsmithline.com)
Adding 3 + 5, Part IV: Browser Search Box (mactipaday.neilsmithline.com)
Correct Decimal To Floating-Point Using Big Integers (exploringbinary.com)













