Command line calculator, bc

September 4th, 2006 mysurface Posted in bc, Calculation, pipeline | Hits: 147216 | 21 Comments »

How to do calculation if I only have command line? If you have BC you can, and you can do a very complicated calculation. To perform calculation, you can type bc and start to type your question, or passing question through pipeline, such as

echo "56.8 + 77.7" | bc

Let say you want to convert decimal to hexadecimal, you can do this.

echo "obase=16; ibase=10; 56" | bc

ibase is input base, and obase is output base, by specified the base, you can convert number from one base to another.

To perform division is a bit tricky, because the will result usually in floating point. Therefore in order to get the correct answer, you need to specified scale. scale means the precision of floating point, how many digit after the point. By default the scale is 0, that means it is integer.
5.00500 – the scale is 5

5.005 – the scale is 3

So the example :

echo "scale=6;  60/7.02" | bc

21 Responses to “Command line calculator, bc”

  1. A simply way to do calculation with floating point

    echo 2/5 | bc -l
  2. bc -l works, but it sets scale=20 which gives:
    1/3
    .33333333333333333333
    most of the time I just want two decimal places.

    there may be an easier way, but what works for me is the following:
    in .bashrc in your home directory add BC_ENV_ARGS=~/.bc
    export BC_ENV_ARGS
    then create .bc also in your home directory
    with one line
    scale=2
    then when bc is run
    1/3
    .33

  3. Cool in that way you can set a default configuration for bc. You can also simply type scale=2 while you are in the interactive mode.

  4. [...] First you need to calculate what time you wanna wake up? Let say if you wanna wake up after 5 hour and 30 minutes. You need to calculate the time in seconds using bc and parse the result to sleep, and later play a song to wakes you up after the sleep: [...]

  5. Jon Alvarado Says:

    To use the last value just use the word last in interactive mode.

    1+1
    >2
    last+2
    >4
    last + 5
    >9
    last +8
    >17
    quit

  6. Nice post. I also use “prepend” to deal with pipeline problems that occur. For example:

    randy@pseudo ~/prng $ echo “13203903″ | prepend “obase=2;ibase=10;” | bc
    110010010111100110111111

    Otherwise, you can’t enter the obase/ibase stuff without messing up the pipeline.

    But as for bc, I don’t use it much, and prefer my “pc” perl calculator. It does normal math, but I mostly use it for advanced financial calculations.

    randy@psion /usr/src $ pc
    Entering interactive mode, use “expression” to run in batch mode.
    >1000000=50000(F/P,i%,30)
    Result: 0.105013847351074 @ 1000003.71937184, aiming for 1000000
    10.50%

    Interpreted: Find the interest rate ‘i’ for which the ‘F’uture value is 1 million given a ‘P’resent value of $50,000 invested for 30 years.

    I think it is up at my website http://nosatalian.homelinux.com

  7. Hi all, great examples here, but I need to compare version no for some programs like bios version 2.3.20 vs 2.3.21, how to do it in bc. thanks

  8. Remember too, that to end a bc session you don’t need to type “quit” (unless you want to). You can simply use CTRL-D. Same for the current shell, by the way. I often open a terminal just for bc. When I’m finished with it I simply tap CTRL-D twice. The first ends bc, the second closes that shell.

  9. echo “obase=36;44444″|bc produces three decimal digits. How do you convert them to YAK which is base36 output in Perl, OO, JS etc., iow, a true base36 number (0-9,a-z).

  10. Another handy way of using bc.

    bc <<< 56.8+77.7

  11. This is great. A built-in calculator that will do calculations in base 6 (or base x) and can easily be set to convert inputs of one base to output of another. It seems that it would be simple to adapt one of the gui calculators based on the bc engine to to the same.

    bc
    scale=30
    obase=6
    1/17
    >.020412245351433102041224535143310204121
    quit

    Well, there is a rounding error on the last “digit” (hexit?). The scale (base 10) is expanded (or contracted) in relation to the output base.

    It looks like bc uses letters for “digits” up to base 16 then resorts to 2-digit pairs. It seems reasonable to adapt it to use letters for “digits” up to base 36.

  12. Hi,
    rtfm:

    snip–
    The legal values of ibase are 2 through 16. Assigning a value outside this range to ibase will result in a value of 2 or 16. Input numbers may contain the characters 0-9 and A-F. (Note: They must be capitals. Lower case letters are variable names.)
    –snap

    some examples:

    10->16:

    ~$ a=$(echo “obase=16; 15+0″ | bc -l)
    ~$ echo $a
    F

    16->10:

    ~$ b=$(echo “ibase=16; obase=A; F+1″ | bc -l)
    ~$ echo $b
    16

    –> note that obase must be given in ibase-style

    10->2

    ~$ c=$(echo “obase=2; 10+0″ | bc -l)
    ~$ echo $c
    1010

    2->10

    ~$ d=$(echo “ibase=2; obase=1010; 10+10″ | bc -l)
    ~$ echo $d
    4

    etc.

    best regards

  13. You might want to save the result of your calculation to a file. This is easy to do with echo and &>.

    echo \\&"scale=1000; sqrt(2)\\&" | bc &> sqrt2.txt

    This will print sqrt (2) to 1000 decimal places to the file sqrt2.txt.

  14. how count all lines in all files in current dir and omit empty lines with wc, grep, cut and bc commands

    echo `wc -l * | grep total | cut -f2 -d’ ‘` – `grep -in “^$” * | wc -l ` | bc

  15. I get pleasure from, lead to I discovered exactly what I was having a look for. You’ve ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye

  16. Hey there, You have done a wonderful job. I will definitely digg it and i really believe recommend to my friends. I am sure they will bebenefited from this site.

  17. Von der Seite habe ich das noch gar nicht gesehen.

  18. Calculate the number of bits needed to represent the number (9.337 * 10^426). NOTE: this number is significant if you follow GRC’s “Off The Grid” password generator, https://www.grc.com/otg/security.htm, it’s the lower bound of the possible number of 26×26 latin squares.

    The calculation to figure out how many bits would be needed to represent the above number, 9.337 * 10^426, is thus:

    echo “l(9.337*10^426)/l(2)”|bc -l
    1418.36432750510331443454

    or roughly 1418 bits.

  19. Thanks for the export BC_ENV_ARGS hint! I had never guessed how to set defaults for bc.

  20. yeah, .bcrc idea helped me get rid of another annoyance as well. scale=2 ftw :>

  21. Mike, maybe this code to work in bashrc without the need to create an extra file:

    BC_ENV_ARGS=$(<<< scale=2) && export BC_ENV_ARGS

Leave a Reply