Skip to Content

Terminal Tip: Command Line Calculator

Mac OS X ships with a powerful and useful command-line calculator called bc. GNU bc provides an arbitrary precision calculator that allows you to type in expressions for immediate calculation. It uses the standard conventions for computer arithmetic, i.e. + and - are addition and subtraction, * and / are multiplication and division, ^ is exponentiation. So to multiple, say, 193 by two thirds, you'd enter 193 * 2 / 3 and press return. Parentheses set the order of evaluation, just as they would in a normal arithmetic statement or in a computer language. e.g. (20 / 3) ^ 5 performs the division before the exponentiation.

You can also use variables with bc. Just assign them using an "=" command. For example, you can set your principal to 100 with principal=100. The special scale variable indicates the number of digits to show after the decimal point. Enter quit to leave bc.

There's a lot more you can do with bc--it's really a full interactive programming language that goes way beyond the simple convenience of quick calculations. To learn about bc, type man bc from the command line.



Categories

Terminal Tips

Mac OS X ships with a powerful and useful command-line calculator called bc. GNU bc provides an arbitrary precision calculator that allows...
 

Add a Comment

*0 / 3000 Character Maximum Comment Moderation Enabled. Your comment will appear after it is cleared by an editor.

9 Comments

Filter by:
Ivan Strand

I do an alias of "calc bc -lq" "l" (ell) adds the math library and sets scale and the "q" turns off the bc's messages. Try bc -lq from your terminal to see how it works.

May 03 2007 at 12:17 AM Report abuse rate up rate down Reply
Chris

The comment gatekeeper ate the link: http://www.postal-code.com/mrhappy/articles/calcinstall.html

April 24 2007 at 2:30 PM Report abuse rate up rate down Reply
Chris

All excellent solutions indeed but if you want something a little more scientific (who doesn't need to be able to calculate pi*e every now and then?) I've written up how to install the Astronomy-aware Unix Calculator (aka 'calc') under OS X here. Pretty handy sometimes.

April 24 2007 at 2:29 PM Report abuse rate up rate down Reply
Sovok

cut me off :/

...bc can even do loops like

for (i=0; i [left_angle_bracket] 10; i++) {
s=s+i
s^2
}

(press Option+Return vor line break in qs text field)

April 24 2007 at 11:19 AM Report abuse rate up rate down Reply
Sovok

Even handier if you use bc via Quicksilver.
Summon Quicksilver, type ., enter your stuff, tab, select "CalcuatorCalculateAction".

bc can even do loops like

for (i=0; i

April 24 2007 at 11:15 AM Report abuse rate up rate down Reply
milosh

 For commandline simple computation, you can use your shell (bash or zsh) ability:
% echo $[2**4096]
0
(yes, it computes modulo 2^32 (or something like that))
 I personnally prefer rpn logic, so I use dc
 To quit bc, you can also ctrl-d.

April 24 2007 at 11:04 AM Report abuse rate up rate down Reply
lar3ry

I've used BC for many years. At one point, it was not just a quick a dirty calculator, but also a quick a dirty command line benchmark program.

For instance:

$ echo '2 ^ 4096' | bc
10443888814131525066917527107166243825799642490473837803842334832839
53907971557456848826811934997558340890106714439262837987573438185793
60726323608785136527794595697654370999834036159013438371831442807001
18559462263763188393977127456723346843445866174968079087058037040712
84048740118609114467977783598029006686938976881787785946905630190260
94059957945343282346930302669644305902501597239986771421554169383555
98852914863182379144344967340878118726394964751001890413490084170616
75093668333850551032972088269550769983616369411933015213796825837188
09183365675122131849284636812555022599830041234478486259567449219461
70238065059132456108257318353800876086221028342701976982023131690176
78006675195485079921636419370285375124784014907159135459982790513399
61155179427110683113409058427288427979155484978295432353451706522326
90613949059876930021229633956877828789484406160074129456749198230505
71642377154816321380631045902916136926708342856440730447899971901781
46576347322385026725305989979599609079946920177462481771844986745565
92501783290704731194331655508075682218465717463732968849128195203174
57002440926616910874148385078411929804522981857338977648103126085903
00130241346718972667321649151113160292078173803343609024380470834040
3154190336

$ echo "2 ^ 4096" | time bc >/dev/null
0.02 real 0.00 user 0.00 sys

(The above timing is from my Mac Mini)

The "benchmark" usage isn't as useful as it was fifteen years ago, since CPUs now measure their speeds in GHZ instead of MHZ...!

April 24 2007 at 10:33 AM Report abuse rate up rate down Reply
mithras

I used to use bc often, but these days I prefer using Ruby's irb or the python interactive shell. They do all the math I need, and have powerful and familiar programming models, too.

April 24 2007 at 9:39 AM Report abuse rate up rate down Reply
Bruce Funk

As a long time *nix usser I always keep a terminal session close to hand and bc is great for the quick computations. and for us geeks, ibase and obase let you select your input and output numerical base, and you have a handy decimal - hex 2*14converter, say...
bc
obase=16
28 (enter)
1C (response)
2*13 (enter)
1A (response)

April 24 2007 at 7:11 AM Report abuse rate up rate down Reply
Buy an ad here

Tweets

© 2012 AOL Inc. All Rights Reserved.