bc is a quirky but useful calculator. It is a standard Unix utility and is also available for Windows.
One nice feature of
bc is that you can set the parameter
scale to indicate the desired precision. For example, if you set
scale=100, all calculations will be carried out to 100 decimal places.
The first surprise is that the default value of
scale is 0. So unless you change the default option, 1/2 will return 0. This is not because it is doing integer division: 1.0/2.0 also returns 0.
bc is computing 1/2 as 0.5 and displaying the default number of decimal places, i.e. none! Note also that
bc doesn’t round results; it truncates.
bc has one option:
-l. This option loads the math library and sets the default value of
scale to 20. I always fire up
bc -l rather than just
The second surprise with
bc is that its math library only has five elementary functions. However, you can do a lot with these five functions if you know a few identities.
The sine and cosine of
x are computed by
c(x) respectively. Angles are measured in radians. There is no tangent function in
bc. If you want the tangent of
s(x)/c(x). (See here for an explanation of how to compute other trigonometric functions.) As minimal as
bc is, it did make a minor concession to convenience: it could have been more minimal by insisting you use sin(π/2 – x) to compute a cosine.
The only inverse trigonometric function is
a(x) for arctangent. This function can be bootsrapped to compute other inverse functions via these identities:
arcsin(x) = arctan(x / sqrt(1 – x2))
arccos(x) = arctan(sqrt(1 – x2 )/ x)
arccot(x) = π/2 – arctan(x)
arcsec(x) = arctan(sqrt(x2 – 1))
arccsc(x) = arctan(1/sqrt(x2 – 1))
e(x) compute (natural) logarithm and exponential respectively.
bc has a power operator
^ but it can only be used for integer powers. So you could compute the fourth power of x with
x^4 but you cannot compute the fourth root of x with
x^0.25. To compute xy for a floating point value y, use
e(l(x)*y). Also, you can use the identity logb(x) = log(x) / log(b) to find logarithms to other bases. For example, you could compute the log base 2 of x using
Not only is
bc surprising for the functions it does not contain, such as no tangent function, it is also surprising for what it does contain. The third surprise is that in addition to its five elementary functions, the
bc math library has a function
j(n,x) t0 compute the nth Bessel function of x where n is an integer. (You can pass in a floating point value of n but
bc will lop off the fractional part.)
I don’t know the history of
bc, but it seems someone must have needed Bessel functions and convinced the author to add them. Without
j, the library consists entirely of elementary functions of one argument and the names of the functions spell out “scale.” The function
j breaks this pattern.
If I could include one advanced function in a calculator, it would be the gamma function, not Bessel functions. (Actually, the logarithm of the gamma function is more useful than the gamma function itself, as I explain here.) Bessel functions are important in applications but I would expect more demand for the gamma function.
Update: Right after I posted this, I got an email saying
bc -l was following me on Twitter. Since when do Unix commands have Twitter accounts? Well, Hilary Mason has created a Twitter account bc_l that will run
bc -l on anything you send it via DM.
Update (November 14, 2011): The account
bc_l is currently not responding to requests. Hilary says that the account stopped working when Twitter changed the OAuth protocol. She says she intends to repair it soon.
How many trig functions are there?
Diagram of Bessel function relationships