Live Chat!

Command line calculator, bc

* * * * * 1 votos

September 4th, 2006 mysurface Posted in Calculation, bc, pipeline | Hits: 10675 |

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

7 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

Leave a Reply