Search

bc: an arbitrary precision numeric processing language

October 11th, 2007 edited by Alexey Beshenov

Article submitted by Markus Grunwald. We are running out of articles! Please help DPOTD and submit good articles about software you like!

Debian currently offers 84 packages that mention “calculator” (apt-cache search calculator | wc -l). An alternative to the more eye-candy-heavy calculators on offer is bc. bc has no fancy GUI, but is instead console-based and allows a simple mathematical language, making it really fast and programmable.

Everyday use

By default, bc behaves quite unhelpfully: zero digits and no predefined functions such as sin, cos, log etc. You can remedy this by calling bc with the -l parameter, so that it uses the standard math library. Instead of always typing “bc -l”, you can use the fact that bc reads the environment variable BC_ENV_ARGS. So for all the bash guys: add an export BC_ENV_ARGS=-l to your ~/.bashrc.

Let’s try the everyday use first: some simple calculations. Just type your expression and complete it with the enter key:

$ 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'.
850*77.1        <enter>
65535.0

That’s better than Excel 2007! And all of you terminal people will get this answer quicker than every KDE or GNOME user who starts their GUI calculator.

Custom scale and functions

The scale (total number of decimal digits after the decimal point) of bc’s results is 20 when you run it with “-l”, but you can set it up to a maximum of 2147483647. So to get a quite good estimate of π, just type:

scale=200  <enter>
4*a(1)     <enter>
3.141592653589793238462643383279502884197169399375105820974944592307\
81640628620899862803482534211706798214808651328230664709384460955058\
223172535940812848111745028410270193852110555964462294895493038196

Impressive. But what’s “a(1)”? bc uses very short names for the trigonometric functions. a(x) is the arctangent of x. For all of you who like longer names, you can define your own function and write it in a file. Add this file to your BC_ENV_ARGS and it will be read every time you start bc. The best thing to do is to get extensions.bc from http://x-bc.sourceforge.net/extensions_bc.html.

My BC_ENV_ARGS looks like this:

$ echo $BC_ENV_ARGS
-q -l /home/gru/.bc/extensions.bc

extensions.bc defines several functions by the names people normally know them, for example:

define sin(x)
{
        return (s(x))
}

This way, you can define your own function. I have to convert ratios to decibel and use this quite often:

define db(x,y)
{
        return 20*log(x/y);
}

Converting to other bases

bc can convert from and to arbitrary number bases. Say you want to convert from decimal to hexadecimal. Set the output base obase to 16:

obase=16 <enter>
10
A

And of course, you can calculate with these numbers too:

10/7
1.6DB6DB6DB6DB6DB6B

You can set the input base too:

ibase=16 <enter>
A/2
5
sin(AFFE) <enter>
-.B1F4021654E454E72

I leave the interpretation to the user ;)

Use in shell scripts

Bash is a nice shell, but when it comes to complex calculations $(()) won’t do the job. bc can help:

$ a=$( echo "l(1024)/l(2)" | bc )
$ echo $a
10

Conclusion

I searched quite long for the ideal calculator for me. After trying several GUI calculators, I didn’t find mine. Then I tried bc and it’s perfect for me. I hope you will like it too.

bc is available from Oldstable to Unstable in Debian and Ubuntu Dapper, Edgy, Feisty and Gutsy.

Links

Posted in Debian, Ubuntu |

4 Responses

  1. puccha Says:

    Hey, thats actually pretty cool! I also found that you can make handy scipts with that (probably in some manual too).

    #!/usr/bin/bc -q

    define succ(x)
    {
    return x+1;
    }

    “2nd succesor of 1 = ”
    succ(succ(1))
    quit

  2. shiny Says:

    Great!

  3. Leandro Penz Says:

    Python can be lauched instantly and can also be used as a calculator.

  4. Orpheus Says:

    Thanks for another entry on this awesome website!

    Actually this is one of the very few tools which I have been using for a longer time. Just in contrast to all those other fancy applications and gadgets I would never have found by myself…

    Btw, I do also appreciate those fun tools like cowtalk/cowthink which was presented some time ago. Since then, a little cow has been reading fortune cookies to me and other server users every six hours ;)…