bc

This post is mainly inspired by this post by Marco, about Soulver, which has, in their words, a “word processor style interface”. I view this as a way to avoid saying “Command Line”, which it basically is.

I’m kind of surprised at this veneration – the concept is nothing new, as there’s already as similar calculator built into Mac OS X and other Unix operating systems, called bc, which is pretty much identical in functionality, with a few quirks.

The first time you load bc up and use it, you’ll probably find that it’s defaults aren’t the best – for example, when dividing a number, you get only the quotient back (the part in front of the decimal place), which isn’t too useful in most cases.

10/3
3

If you set the “scale” variable it will return a proper float with that number of digits:

scale=5
10/3
3.33333

Also, the gnu version of bc prints a gnu advertisement at the front of every session:

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

The fix for this is as follows – bc (at least the common gnu version) will take a set of arguments from an environmental variable (set in your .profile or similar shell init script) like so:

BC_ENV_ARGS='-q /Users/username/.bc'

The -q suppresses the advert, and the path to my .bc file is a set of commands that is executed before every session. Note that it needs a full path supplied – you can’t use ~ to specify your home folder, as bc doesn’t perform shell expansion on it’s arguments passed in this way. My .bc file is simple – it just sets the scale variable:

scale=5

You could also predefine functions and variables as well in there if you cared to.


About this entry