bc: A Handy Utility

Mr. McAndrew shows us how the bc command can be used for prototyping numerical algorithms.

In my opinion, bc is a real find: it is small, efficient, self-contained and an extremely useful utility. It is not to be considered a replacement for a good fast programming language such as C, C++ or FORTRAN. But as a means for quickly prototyping numerical algorithms before coding them in a high-level language, it is excellent.


Alasdair McAndrew lives in Melbourne, Australia, with his wife, three young children and a grumpy cat. He is a Senior Lecturer at Victoria University of Technology, where he teaches mathematics and computing. He is an enthusiastic and satisfied user of Linux, and has been since kernel 0.99; currently he is running Linux on both a desktop and a laptop. He enjoys trawling the Internet for Linux software suitable for children, and when he has time, playing the viola da gamba.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

If you are running the

HL's picture

If you are running the "Hofstadter's chaotic function" with the remainder part, please notice that the BC-utility has a bug with the -l option. It works fine if you use only "bc"-command, instead of "bc -l".

Tested on Ubuntu 9.04.

Clarification about "bc -l"

HL's picture

If you try the code with "bc -l", please note that you must set "scale=0". Otherwise, things such as modulo may seem to work unexpectedly. More here, accessed 28th August 2009 10:14, http://en.wikipedia.org/wiki/Bc_programming_language#Standard_library_fu....

Small notice. When you

HL's picture

Small notice.

When you copy-paste the amicable-code, remove the enter like:

define amicable(m) {
for (j=1;j<=m;j++)
if (sf(sf(j))==j && sf(j)!=j && j

Small error in the code: for

Anonymous's picture

Small error in the code:

for (i=1; i<=100; i++) {
print h[i]," ";
if (i%10==0) print "\n;"

The "\n;" should be "\n"; .